<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Specify size of enum? in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152049#M3715</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm using codewarrior 10 for the MC9S08SH8. I've got a union producing an issue which took a while to track down, and stems from the size of an enum. The size of an enum is compiler-specific and should default to the smallest integral type which is large enough to fit all of the values, but not larger than int. In this case, it seems the sizeof enum is fixed at 16-bit. For starters, that wastes a heck of a lot of memory if you use a boolean typedef (which I do). The real problem was caused by this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;typedef enum { SOME_COMMAND=0x00, ...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SOME_OTHER_COMMAND=0x0F } Commandt;typedef union{ UINT8 s[4]; struct {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Commandt command;&amp;nbsp; UINT16 address;&amp;nbsp; UINT8 data; } l;} Packet;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I expected this to allow me to write the raw bytes and then efficiently access the contents, but of course the CMD_Commandt overlapped the high byte of the address and broke the code. I've managed to correct the issue with the type sizes options in the build settings, but it's not exactly portable code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a #pragma option to set the size of an enum, or better yet, some way to force it to always use the smallest number of whole bytes for an enum?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd really like it to be able to use the smallest number of &lt;EM&gt;bits&lt;/EM&gt; for an enum, and hence stack multiple boolean variables in a bitfield, but I suspect that's a bridge too far &lt;A href="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" rel="nofollow noopener noreferrer noopener noreferrer" target="_blank"&gt;&lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;&lt;/A&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 29 Oct 2020 08:55:54 GMT</pubDate>
    <dc:creator>LethalCorpse</dc:creator>
    <dc:date>2020-10-29T08:55:54Z</dc:date>
    <item>
      <title>Specify size of enum?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152049#M3715</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm using codewarrior 10 for the MC9S08SH8. I've got a union producing an issue which took a while to track down, and stems from the size of an enum. The size of an enum is compiler-specific and should default to the smallest integral type which is large enough to fit all of the values, but not larger than int. In this case, it seems the sizeof enum is fixed at 16-bit. For starters, that wastes a heck of a lot of memory if you use a boolean typedef (which I do). The real problem was caused by this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;typedef enum { SOME_COMMAND=0x00, ...&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SOME_OTHER_COMMAND=0x0F } Commandt;typedef union{ UINT8 s[4]; struct {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Commandt command;&amp;nbsp; UINT16 address;&amp;nbsp; UINT8 data; } l;} Packet;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I expected this to allow me to write the raw bytes and then efficiently access the contents, but of course the CMD_Commandt overlapped the high byte of the address and broke the code. I've managed to correct the issue with the type sizes options in the build settings, but it's not exactly portable code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a #pragma option to set the size of an enum, or better yet, some way to force it to always use the smallest number of whole bytes for an enum?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd really like it to be able to use the smallest number of &lt;EM&gt;bits&lt;/EM&gt; for an enum, and hence stack multiple boolean variables in a bitfield, but I suspect that's a bridge too far &lt;A href="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" rel="nofollow noopener noreferrer noopener noreferrer" target="_blank"&gt;&lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;&lt;/A&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 08:55:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152049#M3715</guid>
      <dc:creator>LethalCorpse</dc:creator>
      <dc:date>2020-10-29T08:55:54Z</dc:date>
    </item>
    <item>
      <title>Re: Specify size of enum?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152050#M3716</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The size of an enum is indeed implementation-defined by the C standard, but practically it is almost always the same as the size of an int. Instead, use this portable version:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;enum {&lt;BR /&gt;&amp;nbsp;SOME_COMMAND=0x00,&lt;BR /&gt;&amp;nbsp;...&lt;BR /&gt;&amp;nbsp; SOME_OTHER_COMMAND=0x0F&lt;BR /&gt;};&lt;BR /&gt;&lt;BR /&gt;typedef UINT8 Commandt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;C doesn't have strong typing anyhow, so the above code is identical to yours, as long as it ain't compiled in C++.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also note that both the union and the struct in your code may have additional padding bytes, so they may not be portable either, depending on how you use them.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Sep 2010 16:46:38 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152050#M3716</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2010-09-30T16:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Specify size of enum?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152051#M3717</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I want a structure containg the data type Commandt, as described in the original posting.&lt;/P&gt;&lt;P&gt;Using the data type in the structure makes it easy to see what data is allowed&lt;/P&gt;&lt;P&gt;just by looking at the typedef of the data type. The original posters problem remains however,&lt;/P&gt;&lt;P&gt;in that twice as much memory as allocated compared to if the enum is only&amp;nbsp; one byte.&lt;/P&gt;&lt;P&gt;(Provided of course that only up to 255 individual values are used).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How can I use the data type to emphasize the type used,&lt;/P&gt;&lt;P&gt;and STILL&amp;nbsp; make sure only 1 byte is allocated in the structure, instead of an entire integer?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anders J&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 Oct 2011 20:17:14 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152051#M3717</guid>
      <dc:creator>AndersJ</dc:creator>
      <dc:date>2011-10-07T20:17:14Z</dc:date>
    </item>
    <item>
      <title>Re: Specify size of enum?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152052#M3718</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The Freescale HCS08 compiler has an option to change the size of an enum: e.g. -TE1ue sets enumeration to 8bit unsigned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;BK&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 07 Oct 2011 22:13:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152052#M3718</guid>
      <dc:creator>BlackNight</dc:creator>
      <dc:date>2011-10-07T22:13:16Z</dc:date>
    </item>
    <item>
      <title>Re: Specify size of enum?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152053#M3719</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;The -TE1ue option forces &lt;STRONG&gt;ALL&lt;/STRONG&gt; the enums to be 8 bit. It would be nice if the compiler could choose the size based on the maximum&amp;nbsp;enumeration value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My (very) old Introl Compiler for the HC11 automatically did that, and porting the code over has caused a few issues with ENUM. Enums made debugging much easier, as the debugger showed the meaningful enum name.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;David&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 10 Oct 2011 19:39:33 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152053#M3719</guid>
      <dc:creator>djsimpson</dc:creator>
      <dc:date>2011-10-10T19:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: Specify size of enum?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152054#M3720</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's a very bad idea to fiddle around with Codwarrior type settings to make your program work. Instead, write portable programs that work on any C compiler.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That way you will also become a better C programmer, rather than a better specific-compiler-programmer.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 26 Oct 2011 19:43:05 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152054#M3720</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2011-10-26T19:43:05Z</dc:date>
    </item>
    <item>
      <title>Re: Specify size of enum?</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152055#M3721</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;LethalCorpse wrote:&lt;BR /&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;I'd really like it to be able to use the smallest number of &lt;EM&gt;bits&lt;/EM&gt; for an enum, and hence stack multiple boolean variables in a bitfield, but I suspect that's a bridge too far &lt;IMG alt=":smileyhappy:" class="emoticon emoticon-smileyhappy" id="smileyhappy" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-happy.gif" title="Smiley Happy" /&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;If the requirement is simply to handle single bit flags in a memory efficient, and code efficient manner,&amp;nbsp;there is an alternative approach is to directly utilize a bit field.&amp;nbsp; With single-bit elements, I do not anticipate portability issues.&amp;nbsp; But maybe others will disagree.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using CW, this method appears to work as anticipated, for both 8-bit and 16-bit variables.&amp;nbsp; For code efficiency on&amp;nbsp;8-bit&amp;nbsp;MCUs,&amp;nbsp;the variable should be placed in zero page RAM.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Within a "common.h" header file, the generic bit fields are defined for both 8-bit and 16-bit types.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;// File: common.h...// Single bit flags:union FLAGSTR8 {   byte B;   struct {      unsigned flag0     :1;      unsigned flag1     :1;      unsigned flag2     :1;      unsigned flag3     :1;      unsigned flag4     :1;      unsigned flag5     :1;      unsigned flag6     :1;      unsigned flag7     :1;   } b;};typedef union FLAGSTR8 FLAGSTR8_;union FLAGSTR16 {   word W;   struct {      unsigned flag0     :1;      unsigned flag1     :1;      unsigned flag2     :1;      unsigned flag3     :1;      unsigned flag4     :1;      unsigned flag5     :1;      unsigned flag6     :1;      unsigned flag7     :1;      unsigned flag8     :1;      unsigned flag9     :1;      unsigned flag10    :1;      unsigned flag11    :1;      unsigned flag12    :1;      unsigned flag13    :1;      unsigned flag14    :1;      unsigned flag15    :1;   } b;};typedef union FLAGSTR16 FLAGSTR16_;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then within a specific .c file, the zero page variable is defined.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#include "common.h"// Global/static variables:#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGEvolatile FLAGSTR16_ flag;             // Single bit control flags...#pragma DATA_SEG DEFAULT&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The corresponding .h header file would then #define the bit usage for the variable.&amp;nbsp; The following example is from one of my projects.&amp;nbsp; Of course, this approach is based on that used by CW to define the various bits of the hardware registers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;#include "common.h"/***********************************************************************/// Global variables:#pragma DATA_SEG __SHORT_SEG MY_ZEROPAGEextern volatile FLAGSTR16_ flag;      // Single bit control flags...#pragma DATA_SEG DEFAULT/***********************************************************************/// Single bit flags:#define flag_COMMAND   flag.b.flag1   // Local command entry mode#define flag_NEWLINE   flag.b.flag2   // New line flag for command entry#define flag_LO_BATT   flag.b.flag3   // Low battery state#define flag_NO_REC    flag.b.flag4   // No record for client code#define flag_WAC_MODE  flag.b.flag5   // Paging message format control#define flag_TSYS      flag.b.flag6   // System timeout flag#define flag_PROMPT    flag.b.flag7   // Previous prompt character#define flag_NORM_S100 flag.b.flag8   // System 100 normal mode#define flag_DFLTPAGE  flag.b.flag9   // Default paging mode flag#define flag_EVPROC    flag.b.flag10  // Default paging event in progress#define flag_DOWNLOAD  flag.b.flag11  // Download mode is current&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 08:55:56 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Specify-size-of-enum/m-p/152055#M3721</guid>
      <dc:creator>bigmac</dc:creator>
      <dc:date>2020-10-29T08:55:56Z</dc:date>
    </item>
  </channel>
</rss>

