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.

Unsigned Char != 8-bits

One of the things that has always confused (or frustrated )me using the F2812 and now the F28335 is that a char is not individually addressable (as far as I know). The smallest unit size for a variable is a 16-bit value. There are so many instances where I want to explicitly use a variable that is only 8-bits (256 values). For instance if I transfer a packed struct via serial port from a PC to the TI Chip and I want to cast that serial byte stream to a struct it doesn't work.

Also another example is say the serial stream has a 1 byte checksum that is also passed along and in the F28335 I re-compute the checksum by adding up the values received. Normally the 8-bit value would overflow above 256 and wrap, however it will not in the TI chip so a comparision at the end won't work unless aditional code is added.

 

What I am getting at is......Is there a compiler switch to be able to make it so a char is 1 byte? Or something else I am missing? Do people just deal with this?

Thanks,

Josh

  • Char is always 1 byte wide - in C nomenclature char/byte is the smallest addressable memory cell, so in F28xxx its 16 bits. There's no way to force compiler to make char 8 bit (half byte). People deal with this by writing smarter functions. Think of translating on both PC and UC to common network format.
  • As mentioned by Bartosz, the smallest addressable memory cell on the F28xx is 16-bits.

    This is mentioned in the TMS320C28x C/C++ Language Implementation User's Guide (SPRU514) in Section 6.3 called Data Types.

    There are compiler instrinsics __byte() and __mov_byte() to access data in increments of 8-bits.

  • This info from the TMS320C28x Optimizing C/C++ Compiler User's guide (SPRU514) may help (page 82):

    By ANSI/ISO C definition, the sizeof operator yields the number of bytes required to store an object. ANSI/ISO further stipulates that when sizeof is applied to char, the result is 1. Since the TMS320C28x char is 16 bits (to make it separately addressable), a byte is also 16 bits. This yields results you may not expect; for example, size of (int) = = 1 (not 2). TMS320C28x bytes and words are equivalent (16 bits).

    To access data in increments of 8 bits, use the __byte() and __mov_byte() intrinsics described in Section 7.4.4.

    Regards

    Lori

  • Thank you,

    This is basically what I understood, however I did not know about the__byte() and __mov_byte() compiler intrinsics. Thanks.

    I'm curious how this design decision got into a TI product. It really makes code portability frustrating. Take for instance this post where he is porting some open source FAT16/32 library code. (http://community.ti.com/forums/t/4554.aspx)

    Since this is a 32-bit CPU why follow standard 32-bit CPU addressing and data sizes?

    Anyways, thanks again and I will deal with it.

    Josh

  • Joshua Hintze said:

    I'm curious how this design decision got into a TI product. It really makes code portability frustrating. Take for instance this post where he is porting some open source FAT16/32 library code. (http://community.ti.com/forums/t/4554.aspx)

    Since this is a 32-bit CPU why follow standard 32-bit CPU addressing and data sizes?

    History, about 15 years of it in fact.  The C2000 core originally was a 16-bit architecture and even then, the smallest addressable unit was the native size of 16-bits.  As the 32-bit architecture was introduced, legacy needed to be maintained for compatibility purposes.

     

    Joshua Hintze said:

    Anyways, thanks again and I will deal with it.

    We certainly appreciate it and hope that although this is a nuisance to your specific task, you will continue to find applications for the C2000 family.

  • I gotcha,

    These forums have been very responsive. Keep up the good work.