Link to home
Start Free TrialLog in
Avatar of Richard Payne
Richard PayneFlag for Bulgaria

asked on

C#, VS15, StructLayout

In C, I have the struct and union in embedded MCU

 
			union {
				uINT32 wSetSerialPolicy;
				struct{
					uINT32 I2C:2;			        // Only for A4/A5 MUX.
					uINT32 SPI0:2;			// SPI0 Bus including SSEL
					uINT32 SPICS:2;			// SPI Chip Select
					uINT32 UART:2;			// UART (TX/RX)
					uINT32 OWI:2;			// OWI  (I/P Only)
					uINT32 SENT:2;			// Sent (I/P Only)
					uINT32 CAN:2;			// Can Bus
					};
				};

Open in new window


The above data will be transferred to C# window (cannot use C++) for remote adjustment. I had a look into StructLayout but unsure if I know what I'm doing.

The question
(1) How to read/write whole 32 bit ie wSetSerialPolicy?
(2) Is there way to do this to read/write whole word wSetSerialPolicyand do discrete adjustment at the bits level.
(3) Below is the code in C# bit unsure if they actually in sync with layout?,
(4) I like to see how to check bits layout this since debug shown sorted order but not in sync with layout.
(4) How to display binary in debug (is there other way to display for Console.WriteLine( "----INFO: {0}", wSetSerialPolicy.ToString() );
(5) I aware of BitValue and BitArray but unsure if this support 2 bits configuration.
(6) I aware of http://stackoverflow.com/questions/14464/bit-fields-in-c-sharp but looking for better way to do this.
(7) I open for solution

     
 
wPolicyMIxedStruct sPolicySerial;

        // ==================================================================Getter/Setter
        public UInt32? wPolicySerial { get; set; }

        [StructLayout(LayoutKind.Explicit, Size = 32, CharSet = CharSet.Ansi)]
        public class wPolicySerialStruct
        {
            [FieldOffset(0)]
            public ushort I2C;
            [FieldOffset(2)]
            public ushort SPI0;
            [FieldOffset(4)]
            public ushort SPICS;
            [FieldOffset(6)]
            public ushort UART;
            [FieldOffset(8)]
            public ushort OWI;
            [FieldOffset(10)]
            public ushort SENT;
            [FieldOffset(12)]
            public ushort CAN;
        }

        // ==================================================================constructor
        public zEVKITPortPolicy()
        {
            sPolicySerial = new wPolicySerialStruct();
            sPolicySerial.OWI= 3;
            sPolicySerial.CAN = 1;
            Debug.WriteLine("---INFO: "+sPolicySerial.ToString());
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial