Layering of Structs...

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

Layering of Structs...

4,755 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Sat Jun 18 14:45:33 MST 2011
Trying to figure out why layering of two structs on top of the same memory space do not result in what I'm expecting....

struct AAA
{
[COLOR=Red]int16_t[/COLOR] aaa;
[COLOR=Red]int16_t[/COLOR] bbb;
[COLOR=Red]int16_t[/COLOR] ccc;
} PACKED ; 

struct AAA   mylistA  __attribute__((at(0x10007000)));

struct BBB
{
[COLOR=Lime]int8_t[/COLOR] aaa_L;
[COLOR=Lime]int8_t[/COLOR] aaa_H;
[COLOR=Lime]int8_t[/COLOR] bbb_L;
[COLOR=Lime]int8_t[/COLOR] bbb_H;
[COLOR=Lime]int8_t[/COLOR] ccc_L;
[COLOR=Lime]int8_t[/COLOR] ccc_H;
} PACKED ; 

struct BBB mylistB  __attribute__((at(0x10007000)));

mylistA.aaa = 100
mylistA.bbb = 101
mylistA.ccc = 102

sprintf(mybuffer, "%i,%i,%i, mylistA.aaa, mylistA.bbb, mylistA.ccc);
// this prints out the correct values = 100,101,102 //

// However //

sprintf(mybuffer, "%i,%i,%i,%i,%i,%i, mylistB.aaa_L, mylistB.aaa_H, mylistB.bbb_L, mylistB.bbb_H, 
mylistB.ccc_L, mylistB.ccc_H);
                                 
// this prints out  = 0,0,0,0,0,0 //


Any insight what I may be doing wrong ??
0 Kudos
Reply
35 Replies

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Fri Jun 24 02:03:10 MST 2011
I understand much better now.
Your help was [U]very[/U] welcome.
I appreciate your foresight in understanding beyond the few clueless words uttered by a n00b.

Thanks TFG,
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri Jun 24 01:47:48 MST 2011
But you are trying to program in C and therefore you need to use C constructs to do it.  Using unions is the PORTABLE way of doing it, meaning it will work in any compiler on any target.

Of course, if you want to use non-portable extensions, then you will have to bear the consequences when you move to a different compiler.

To summarise:
- you can do what you are trying to do by using unions
- you can 'simplify' your coding by using macros that references those unions
- all of this is portable and can be used with any C compiler and any architecture
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Fri Jun 24 01:15:46 MST 2011
:eek::eek:

What I really wanted to mean, is by just making the different arrays start co-incidentally by a directive like 'Absolute'.

TFG... thanks for your very kind help. !!
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri Jun 24 01:08:34 MST 2011

Quote:

One cannot overlay [I]arrays[/I] of equal length but different types.

Where did you get that from? That is exactly what zero showed you. Another example:
union {  
unsigned char bytes[1024];
unsigned short halfwords[512];
int words[256]; 
} bytes_halfwords_words; 
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Fri Jun 24 00:19:04 MST 2011
Hi Zero,

WOW.. you get up early in the morn !!!:eek::eek:

Thanks for your reply !!!!!

Ok..that means forget all that I wrote before.
One cannot overlay [I]arrays[/I] of equal length but different types.
It just cannot be done simply with inline code, or with how I was intending.

I have to use union/structs.

Therefore the special 'byte array' underlying everything else in my variables would be:

BEER(x)


because, you defined the whole array with the very friendly name BEER...
#define BEER(x)      mixed.be[x].beer

OK.. let me see.
Thanks Zero.




Barman !!...... Two BEERs for Zero!!
[IMG]http://img219.imageshack.us/img219/4528/bieres.png[/IMG]
[IMG]http://img219.imageshack.us/i/bieres.png/[/IMG]
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Jun 23 18:37:35 MST 2011

Quote:

[B]...share same memory space[/B]...:confused:

Thats exact what unions are made for :eek::mad::eek:

//define first structure
typedef struct
{
 union
 {
  volatile int16_t apples;
  struct
  {
   volatile uint8_t apples_lo;
   volatile uint8_t apples_hi;
  };
 };
}t_apples;

//define second structure
typedef struct
{
  volatile uint8_t beer;
}t_beer;

//mixed union
union
{
 t_apples ap[10];
 t_beer be[20];
}mixed;

//easy access
#define APPLES(x)    mixed.ap[x].apples
#define APPLES_LO(x) mixed.ap[x].apples_lo
#define APPLES_HI(x) mixed.ap[x].apples_hi
#define BEER(x)      mixed.be[x].beer
Usage:
 APPLES(0) =  32767;
 APPLES(1) = -32767;
 APPLES_LO(6) = 0x12;
 BEER(12) = 0x44;
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Thu Jun 23 17:20:41 MST 2011
If I use
typedef struct
{
 union                                   
 {
  __IO int16_t Apples;
  struct                          
  {
   __IO uint8_t apples_lo;
   __IO uint8_t apples_hi;
  }byte1;                                    
 }int1;    

 union                                   
 {
  __IO int16_t Beer;
  struct                          
  {
   __IO uint8_t beer_lo;
   __IO uint8_t beer_hi;
  }byte2;                                    
 }int2;                              
                                   
} MY_FULL_FUNKY_STRUCT_TypeDef;

 MY_FULL_FUNKY_STRUCT_TypeDef ApfelBeer;  // :):):)
:D:D:D:D

Then if :

ApfelBeer.Apples = 1111;
ApfelBeer.Beer = 2222;

//Bytes result in proper Hi and Low Byte values.... mapped well.

[B]//@ZERO ---  Map file shows only the 'ApfelBeer' and its size is 4 ( correct ).[/B]
OK.. all good.




Glad that this works....but..this is not what I originally intended to achieve.

At least through everyone's help, I learned my first step with unions and structs. Something I was really afraid to use before.

[B]Thanks CodeRedSupport, Zero, TheFallGuy[/B]!!!!

But for what I [I]need[/I] to do, union/structs complicates life and is not what I intended in the first place.

When I posted the first post, I was thinking .. perhaps there's an __attribute__ function (or a combination of other intelligent hacks ) that allows me to achieve my aim:

/* [B]to overlap / overlay / mirror / co-locate / share same memory space[/B] */

 
volatile int16_t   APPLES[10];  

// with

volatile uint8_t BEER[20];

//so that if 

APPLES[[COLOR=SeaGreen]0[/COLOR]] = 32767;

// then
//****AUTOMATICALLY****//
//
BEER[[COLOR=SeaGreen]0[/COLOR]] = 0xFF;
//and
BEER[[COLOR=SeaGreen]1[/COLOR]] =0x7F;

//and if 

APPLES[[COLOR=Magenta]1[/COLOR]] = -32767;

// then
//****AUTOMATICALLY****//
//
BEER[[COLOR=Magenta]2[/COLOR]] = 0x01;
//and
BEER[[COLOR=Magenta]3[/COLOR]] =0x80;

Whether used on GNU compilers that do not support the
__attribute__((at(0xXXXXXXXX)))

...or others that allow me...
to map my data using this simple and lovely variable attribute,

I wish to do simply the above.... the same as I used to do with my old 8bitters....
worked so efficient for me...... sigh.....

!!help!!
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Thu Jun 23 14:55:24 MST 2011
Sorry, it's dark outside and his mom doesn't allow him to play outside then :)

So did you already read your map file to discover how much memory is allocated for your 'union1'?


Quote:

Allocating common symbols
Common symbol       size              file
_extra              0x4 
[COLOR=Red]union1              0x30              ./src/main.o[/COLOR]
__heaps             0x4
__end_of_heap       0x4
__Ciob              0x140
status              0x1               ./src/main.o

0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Thu Jun 23 13:11:15 MST 2011
Hey Zero, can you send me your trainee that solved this in 30 minutes ??

Still no joy with my 'parallel and overlying different type arrays' ... Argghh!!
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Jun 21 16:35:09 MST 2011
If you mention a free camp bed and all this in Paris ( =France, not somewhere in the middle of nowhere), that shouldn't be a problem :):eek:
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ktownsend on Tue Jun 21 16:26:01 MST 2011

Quote: Zero
Checked that today and in fact, my 17 year old trainee found the problem within 30 minutes and could describe it. Needless to mention that he's been tortured through all basic issues. So he knows what a pointer is and how to use a debugger.



Wait ... how the heck do *I* get a trainee! ;)

Trainee wanted: Benefits include free hours on the pick and place machine, fresh croissants every morning, and the right to raid the parts cabinets. :D
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Jun 21 15:07:00 MST 2011

Quote:
A pointer is a single machine, and cannot fetch (or prefetch ) two things at once.



Wrong answer :eek:

Wiki:

Quote:

Pointer (computing)
Pointer a pointing to the memory address associated with variable ...
In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

So first question while using pointer is: which address is it pointing to :confused:
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Tue Jun 21 14:57:33 MST 2011
Ok.. that I probably found out why.....
A pointer is a single machine, and cannot fetch (or prefetch ) two things at once.

Back to working with previous attempt of unions+ structs under a big struct.
That's working, and I can see from the debugger, the structure exactly as it is...

The only limitation I'm finding is that I cannot do absolute ( manual ) placing of variables. That would have been so utterly simple to overlay the two arrays of different composition, but equal total length.
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Jun 21 14:49:19 MST 2011

Quote:

...trying to figure out why my_funny_struct...

That's easy to answer: You're still running in C-P mode. Without understanding basics your skills are very limited. Checked that today and in fact, my 17 year old trainee found the problem within 30 minutes and could describe it. Needless to mention that he's been tortured through all basic issues. So he knows what a pointer is and how to use a debugger.
Your ghostwriter served a good idea, unfortunately with a pointer and all its typical problems :eek:
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Tue Jun 21 14:16:46 MST 2011

Quote: Zero
I'll drink R74/12, if it's ready in 2 weeks.



Nope.. won't be ready.:D

I drink Xpresso every evening.
It keeps me up late at night, everyday, trying to figure out why [I][B]my_funny_struct
[/B][/I]is not working well.

I didn't choose to work with my emmBed, even though Libraries are a commodity much appreceated with n000bs like me.

Zero, thanks for your reply last Sonntag afternoon. Didn't expect that so quick.

One further question.
Can one [I]enum [/I]two identical arrays ??

Edit - self reply - Nope.
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Tue Jun 21 13:54:26 MST 2011

Quote:

...what beer r u drinking ??

I'll drink R74/12, if it's ready in 2 weeks. Until then I'm drinking Espresso, which helps my brain to understand why funny C-P codes are not working :eek:
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Mon Jun 20 12:10:30 MST 2011
C-P ( thanks for the wiki link -> interesting reading ). The pejorative connotation is not what I'm doing.

Of course, in [I][B]my_funny_struct_logic.h[/B][/I] file , I am using
#define __PACKED __attribute__((__packed__))
since the code example on Page 1.

Zero...what beer r u drinking ?? Must be more bitter than usual today .. is it ??
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Jun 20 11:45:27 MST 2011

Quote:

I love your replies...

OK, then I'll repeat them:

Quote:

Is there someone out there, who is reading manuals or Wikis?

http://support.code-red-tech.com/Cod.../PackedStructs

There's described the correct syntax: __attribute__((packed)) :eek::eek:

Are you using this syntax?

C-P is what you are doing: Copy and paste programming:

http://en.wikipedia.org/wiki/Copy_and_paste_programming
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Wenzu on Mon Jun 20 11:34:09 MST 2011
Hi Zero, Thanks for popping by.
I love your replies ...:o

Yes.. So.. a Pointer is the number on the lottery ticket you have just purchased.
The contents of the pigeon hole that has the bounty have nothing to do with your ticket.
But when the draw comes up, if your pointer matches that on the winning drawn number you see on the screen,  then you have access to the contents of the pigeon hole. How 'bout that ??

Now... what is C-P ??

I've also tried without __Packed.. still the same hardfault error.

Isn't __packed, supposed to guarantee that there will be no empty pigeon holes ,
...so that if your ticket has that unlucky number that is'skipped' in the 'non-packed' stack, then you wouldn't be fighting with the lottery organizer that he tricked you into buying a false ticket ??

:D:D
0 Kudos
Reply

4,090 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Jun 20 11:14:13 MST 2011
1. The sample above is using a pointer. Do you know what that is ?

2. The sample above is no C-P sample. As mentioned above syntax '__packed' is not working and therefore destroys your anonymous union :eek:
0 Kudos
Reply