Cpu Trap instructionError when I cast byte array to struct

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
User22282
Level 1
Level 1
Hello!
I am trying to rewrite my code from other MCU to TC234. And when i try to cast Can bus data to (uint16_t) like this:

- uint16_t var = *(uint16_t*)pCanData;

CPU goes to IfxCpu_Trap_instructionError(). And the same happends if i try to write field of casted struct like this:

Header_t *pxHeader = (Header_t *)byteArrayGetData(&pCtx->byteArray);
pCtx->sessionCnt = pxHeader->sessions;

What i do wrong, or TC234 can't do this operations?
0 Likes
1 Solution
MoD
Employee
Employee
50 likes received 500 replies posted 100 solutions authored
You can change the definition e.g. uint8_t canData[8] IFX_ALIGN(IFX_ALIGN_16), then canData[0] is 16 bit aligned.
Same works for the byteArray. If you add an IFX_ALIGN(IFX_ALIGN_32) then the struct is 32 bit aligned.
Maybe there is also a compiler option to use always 32 bit address for begin of structs and fields. Please check the compiler documentation for such a setting if exist.

View solution in original post

0 Likes
4 Replies
MoD
Employee
Employee
50 likes received 500 replies posted 100 solutions authored
This will be happen when e.g. pCanData is not 16 bit aligned. Check the TIN of the trap, I expect it is 4 (ALN - Data Address Alignment).
Same occurs with the struct. You must make sure that the address of the byteArray is 32 bit aligned.
0 Likes
User22282
Level 1
Level 1
Thank you for answering!

TIN of the trap is realy 4. If I understand it right uint8_t canData[8] most be 16bit aligned;
so it's no way to do forward cast from canData to uint16_t like this

*(uint16_t*)&canData[0], the only way is used bit shift - (canData[0] | canData[1] << 😎

howeever how can I sure that adress of byteArray is 32 bit aligned? Is that some compiler setting? Or I most create all structures with padding.
0 Likes
MoD
Employee
Employee
50 likes received 500 replies posted 100 solutions authored
You can change the definition e.g. uint8_t canData[8] IFX_ALIGN(IFX_ALIGN_16), then canData[0] is 16 bit aligned.
Same works for the byteArray. If you add an IFX_ALIGN(IFX_ALIGN_32) then the struct is 32 bit aligned.
Maybe there is also a compiler option to use always 32 bit address for begin of structs and fields. Please check the compiler documentation for such a setting if exist.
0 Likes
User22282
Level 1
Level 1
Thank you for answer!
0 Likes