union value issue

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

union value issue

Jump to solution
2,034 Views
赵子成
Contributor IV

Hi, everyone,

Now I use S32K146, I define a union type variable as follow:

typedef union unCanFrameData
{
UINT8 u8Data[CAN_FRAME_DATA_LEN]; ///< 8 uint8 data
UINT16 u16Data[4]; ///< 4 uint16 data
UINT32 u32Data[2]; ///< 2 uint32 data
}UN_CAN_FRAME_DATA;

But when I set the u16Data[2]:

unData.u16Data[2] = 2000

I get the follow result:

pastedImage_1.png

The problem is that the u16Data[2] is 2000, but the u8Data[4] and u8Data[5] is 0, the data is not Coincident.

Can anyone help me? Thanks.

Tags (3)
1 Solution
1,844 Views
赵子成
Contributor IV

Hi Erich.

I have solved this issue.

The root is that I define UINT16 as unsigned int.

In S32K CPU, unsigned int is 4 bytes long. Not 2 bytes.

This make the union alain failure.

Thanks.

View solution in original post

5 Replies
1,844 Views
1642280511
Contributor I

hello,I'm Chinese people,if you too,can you tell me your qq number,I'd like to ask you some questions。thank you so mach.

0 Kudos
1,844 Views
赵子成
Contributor IV

Hi, I am sorry. I didn't use QQ.

I you would, you can ask me via this platform.

0 Kudos
1,844 Views
BlackNight
NXP Employee
NXP Employee

I quickly tried that, but I get a reasonable result:

pastedImage_1.png

with

  typedef uint8_t UINT8;
  typedef uint16_t UINT16;
  typedef uint32_t UINT32;

#define CAN_FRAME_DATA_LEN 8
  typedef union unCanFrameData
  {
  UINT8 u8Data[CAN_FRAME_DATA_LEN]; ///< 8 uint8 data
  UINT16 u16Data[4]; ///< 4 uint16 data
  UINT32 u32Data[2]; ///< 2 uint32 data
  }UN_CAN_FRAME_DATA;

and

unData.u16Data[2] = 2000;

I believe it could be some compiler settings you are using (can you share a small example project)?

I used the S32K144 (don't think it matters), but can you check that 0x2000'ef58 you indeed have RAM?

I hope this helps,

Erich

0 Kudos
1,845 Views
赵子成
Contributor IV

Hi Erich.

I have solved this issue.

The root is that I define UINT16 as unsigned int.

In S32K CPU, unsigned int is 4 bytes long. Not 2 bytes.

This make the union alain failure.

Thanks.

1,844 Views
赵子成
Contributor IV

Hi Erich,

I think it could be something about union settings. Even I set the union variable to be global, it happen this error again:

Could you tell me where to set the union or the data type setting?

Thanks. 

0 Kudos