AVR32 UC3 gcc 4.4.7 from ΑS6.2sp1: Can't make __((parked, aligned(1))__ work.

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Freaks! I need your help because i am stuck!

 

I am reading a BMP file from SD card to SDRAM char buffer succesfully in my EVK1105

 

Then i want to copy the first 14bytes of that buffer or actually link to a pointer struck of the .bmp header struckt as you can see:

typedef struct __attribute__((parked, aligned(1))){
	unsigned short int  Signature;
	unsigned int		Size;
	unsigned short int  Reserved1;
	unsigned short int  Reserved2;
	unsigned int        BitsOffset;
}BITMAP_FILEHEADER;

typedef struct __attribute__ ((parked, aligned(1))){
	unsigned int HeaderSize;	 /* Size of info header */
	int Width;					 /* Width of image */
	int Height;					 /* Height of image */
	unsigned short int Planes;	 /* Number of color planes */
	unsigned short int BitCount; /* Number of bits per pixel */
	unsigned int Compression;	 /* Type of compression to use */
	unsigned int SizeImage;		 /* Size of image data */
	int PelsPerMeterX;			 /* X pixels per meter */
	int PelsPerMeterY;			 /* Y pixels per meter */
	unsigned int ClrUsed;		 /* Number of colors used */
	unsigned int ClrImportant;	 /* Number of important colors */
	unsigned char palette[1024]; /* Storage for palette       */
	/*unsigned int RedMask;
	unsigned int GreenMask;
	unsigned int BlueMask;
	unsigned int AlphaMask;
	unsigned int CsType;
	unsigned int Endpoints[9]; // see http://msdn2.microsoft.com/en-us/library/ms536569.aspx
	unsigned int GammaRed;
	unsigned int GammaGreen;
	unsigned int GammaBlue;*/
}BITMAP_HEADER;

I added on those structs the above to be able pack and align in bytesthe uint8_t sdram buffer data to the struct.

 

__attribute__ ((parked, aligned(1))

It didnt work the compiler shows the above so it doesnt work as i also noticed with my JTAGICE3 in debugging:

Warning    1    'parked' attribute directive ignored    D:\Atmel Studio 6.2 Projects\JPEG_PRJ\IJG_EXAMPLE1\src\bmp.h    25    1    JPEG_PRJ

 

More details:

the first bytes of my BMP file (seen from a HEX editor):

42 4d 36 e2 b9 00 00 00 00 00 36 00 00 00 28 00

which 42 4d is the first unsigned short int  Signature;

So the next 4 bytes that should be in unsigned int        Size;

are not copied or pointed correctly so i should see 36 e2 b9 00 00 but i actually see b9 00 00 00 in my unsigned int        Size of the struct because i doesnt align them. WHY?

 

The above is from UC3 manual

 

Unaligned Reference Handling
AVR32UC does not support unaligned accesses, except for doubleword accesses. AVR32UC is
able to perform word-aligned st.d and ld.d. Any other unaligned memory access will cause an
address exception. Doubleword-sized accesses with word-aligned pointers will automatically be
performed as two word-sized accesses.
The following table shows the instructions with support for unaligned addresses. All other
instructions require aligned addresses.
Table 4-1. Instructions with Unaligned Reference Support
Instruction Supported Alignment
ld.d Word
st.d Word

 

What that means? Its not possible to do that?

i also tried with the -fpack-struct but my code doesn't work at all! it stucks in the pll lock loop of ASF...

 

The only working idea that i have is to make a stupid function like this to interpret and return back a copy of the useful data in the correct types but its really ugly

 

uint32_t read(char * buffread, bytes_enum_t bytes)
{
    uint8_t x[4] = { 0 };
    
    for(int i =0; i<bytes; i++)
    {
        x[i] = *(buffread+i);
    }
    
    
    if(bytes == WORD_bytes)
        return (uint32_t) ((uint16_t) x[0] | ((uint16_t)(x[1] << 8)));
    
    else if(bytes == DWORD_bytes)
        return (uint32_t) ((uint32_t) x[0] | ((uint32_t)(x[1] << 8)) | (((uint32_t)(x[2] << 16))) | ((((uint32_t)(x[3] << 24)))));
    else
        return 0;    
}

 

Last Edited: Fri. Nov 14, 2014 - 10:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You probably want to

__attribute__ ((packed, aligned(1))

and not

__attribute__ ((parked, aligned(1))

 

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OMG! I am so stupid! Thank you JW

This is a reason why we should stop programming at late nights and continue in the morning!

 

BTW whats the point of -fpack-struct and why it doesnt work for me? Is this some kind of automatic __attribute__ ((packed, aligned(1)) so i dont have to write it on every struct?

 

wek3 wrote:

You probably want to

__attribute__ ((packed, aligned(1))

and not

__attribute__ ((parked, aligned(1))

 

JW

Last Edited: Fri. Nov 14, 2014 - 10:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> whats the point of -fpack-struct

> Is this some kind of automatic __attribute__ ((packed, aligned(1)) so i dont have to write it on every struct?

 

Yes

 

https://gcc.gnu.org/onlinedocs/g...

 

> and why it doesnt work for me?

Dunno. It might have packed a struct used in a externally linked library which anticipates unpacked struct, for example. I don't AVR32.

 

Btw., there's a gcc-vs-microsoft gotcha with structs packing, it should not apply to avr32-gcc (it does not to avr-gcc), but better be aware of the potential problems.

 

Plus the endiannes problem with structs/bitfields in genera.

 

JW

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Exactly!

Now my biggest problem is the endiannes. The data of the char buff is in little endian but the CPU is big endian so i get all the data swapped.

I assume the only solution is to manually swap them as i can see there are swap macros in ASF\avr32\utils\compiler.h

 

wek3 wrote:

> whats the point of -fpack-struct

> Is this some kind of automatic __attribute__ ((packed, aligned(1)) so i dont have to write it on every struct?

 

Yes

 

https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fpack-struct-2696

 

> and why it doesnt work for me?

Dunno. It might have packed a struct used in a externally linked library which anticipates unpacked struct, for example. I don't AVR32.

 

Btw., there's a gcc-vs-microsoft gotcha with structs packing, it should not apply to avr32-gcc (it does not to avr-gcc), but better be aware of the potential problems.

 

Plus the endiannes problem with structs/bitfields in genera.

 

JW

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

DieCore wrote:
I assume the only solution is to manually swap

Yes, that is the "proper" way to do it.

 

This is known as serialisation - although commonly practised, trying to do this by forcing a struct on the raw byte stream is really an abuse of the struct - for the reasons you have discovered!

 

Far better to have a function the pulls the bytes from the raw stream into a struct, and another function to take bytes and puts them into the stream.

 

http://en.wikipedia.org/wiki/Serialization

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...