[Question] Why IICDATA uses ALIGN_NONE

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

[Question] Why IICDATA uses ALIGN_NONE

Postby topikachu » Wed Sep 18, 2013 3:02 pm

As I check the IICDATA uses ALIGN_NONE http://sourceforge.net/p/lejos/ev3/ci/m ... t.java#l52
Is there any reason that IICDATA has no align?
topikachu
Novice
 
Posts: 32
Joined: Wed Sep 18, 2013 3:27 am

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby gloomyandy » Wed Sep 18, 2013 3:28 pm

Yes if it doesn't things don't work! None of the other alignment options seem to match the alignment used by the Lego kernel modules. ALIGN_NONE allows us to control the actual alignment needed. Why do you ask? Have you found this not to be the case?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3646
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby topikachu » Wed Sep 18, 2013 3:43 pm

Just wondering.
Because in your code, all other structs are in default align.
For example, AnalogPort size is 5172 by default align and this work.
Totally confused.
topikachu
Novice
 
Posts: 32
Joined: Wed Sep 18, 2013 3:27 am

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby gloomyandy » Wed Sep 18, 2013 4:02 pm

The LocalAnalogPort code does not use a Structure class because it only uses memory mapped data which is not easily (or at all) accessed with Java Structures. The LocalUartPort which does use the Structure class also has the alignment set to none. What other "structs" are default aligned that are actually mapped to data used by the kernel modules?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3646
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby skoehler » Wed Sep 18, 2013 4:53 pm

The layout of the structure is (int, byte, byte, short, byte, byte[], byte, byte[]). I don't see where C compiler would add padding, regardless of whether the structure would use the default alignment rules or not, unless the two arrays are pointers in the C struct. Maybe this is a JNA bug and JNA alignes the two array incorrectly?
skoehler
leJOS Team Member
 
Posts: 1350
Joined: Thu Oct 30, 2008 4:54 pm

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby gloomyandy » Wed Sep 18, 2013 5:25 pm

Hi Sven, you may well be correct that it is not needed in this case, however it certainly works. The problem I hit was in the LocalUARTPort code which has more complex nested structures and that certainly did not align with the actual data with any setting other than ALIGN_NONE. When I came to do the other classes I simply re-used the settings as it seems easier than trying to work out what the jna stuff is doing. I'm not sure what settings exactly are used to build the modules but they seem to be packed. To be honest none of this is a big deal compared to the way I'm currently having to handle the structures mapped directly into memory. That is really ugly. I couldn't find a better way to do it and I looked pretty hard.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3646
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby skoehler » Wed Sep 18, 2013 6:08 pm

Oops, so I looked at the wrong code. So JNA should get the alignment of TYPES and UARTCTL structures right out of the box, unless it has a bug of course. Now with ALIGN_NONE, the actual size of the Java version of UARTCTL should be two bytes less than the size of the C-version of UARTCTL. The thing is, that the alignment of the members of a struct also affects the size of the struct which is always padded to be a multiple of the largest alignment of any member of the struct. Now on the Java end, you say the alignment of all members is one. I guess, since it's only 2 bytes, this doesn't really yield any bugs. But it's dangerous nonetheless. Could you add a comment that we remember to look into this? I couldn't find any special attributes or pragmas in Lego's headers.
skoehler
leJOS Team Member
 
Posts: 1350
Joined: Thu Oct 30, 2008 4:54 pm

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby gloomyandy » Wed Sep 18, 2013 10:06 pm

OK now I'm very puzzled. I've just been revisiting the UART code so while there I changed the alignment back to default and it works! I have no idea what is going on here because I know that was not the case when I originally tried all of this. Oh well more testing in order I think. I'll try switching the i2c code at some point as well.
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3646
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby topikachu » Thu Sep 19, 2013 12:57 am

All structs should be in same align rule. And I don't find any kernel compile flag to change the default rule. Andy would you please try the default align again?

BTW, I bought one EV3, but it's still on the way. I can't test now. :(
topikachu
Novice
 
Posts: 32
Joined: Wed Sep 18, 2013 3:27 am

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby gloomyandy » Thu Sep 19, 2013 7:36 am

See my previous post I have just reverted to using the default packing and for some reason it now seems to work. But that was definitely not the case when I first put this code together. Trust me I would not have bothered going through all the hassle of changing the alignment if I didn't need to. Anyway when I get chance I'll also change the i2c code and check that it still works.

Anyway what exactly are you planning to do?
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3646
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: [Question] Why IICDATA uses ALIGN_NONE

Postby topikachu » Thu Sep 19, 2013 8:00 am

Just for fun!
I buy one ev3 (not arrive yet), and want to use java.
I want to understand the code, so I investigate yours. :D
topikachu
Novice
 
Posts: 32
Joined: Wed Sep 18, 2013 3:27 am


Return to EV3 Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff