This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TMS320F28379D: Issue when typecasting

Part Number: TMS320F28379D

Tool/software: Code Composer Studio

We are using TMS320f28379D eval board. I'm facing issue when typecasting lacal 16-bit array to 32-bit variable.

unsigned short int arr[5] = {0x1234, 0xabcd, 0x12ab, 0x5577, 0xaa55};


unsigned int b;


b = *(unsigned int*)&arr[0];

I'm expecting value in b is 0xabcd1234. But it is fetching from previous memory word and the value of b is 0x1234ffff. In expression window  (*(unsigned int*)&arr[0]) it is showing the as expected (0xabcd1234).

  • In the C2000 compiler, the type int is 16-bits wide.  So I don't know how this ...

    Subash Sundaresan said:
    the value of b is 0x1234ffff

    ... can be the case.  For 32-bit wide integer values, use the type long.

    But an even better solution is to use the fixed-width types supplied in the standard header file stdint.h.  In this case, you would use uint16_t and uint32_t.  Please learn more about stdint.h from your preferred book, website, etc. on the C programming language.

    Another detail needs attention.  The alignment of the array may be wrong.  Recall that the C28x CPU addresses memory as 16-bit words, and not 8-bit bytes.  An array of 16-bit wide elements may start on an odd or even word address.  All 32-bit wide data types (long, float) must be aligned to an even word address.  If the array starts on an odd address this code ...

    b = *(uint32_t *)&arr[0];

    ... does not work.  To avoid this problem, use #pragma DATA_ALIGN to align the array on even word boundary ...

    #pragma DATA_ALIGN(arr, 2)
    uint16_t arr[5] = {0x1234, 0xabcd, 0x12ab, 0x5577, 0xaa55};
    

    Please search for DATA_ALIGN in the C28x compiler manual.

    Thanks and regards,

    -George