Topic : Can enum data types be of different sizes?

Forum : ST7/STM8

Original Post
Post Information Post
January 6, 2010 - 5:47pm
Guest

Is there a a way to define an enumerated type that uses only 8 bits without affecting all enumerated types?

Our project has small RAM constraints, so I wanted to use an enum type to track a state variable (only has about 32 states). However, other STM8 library code needs 16-bit enums; for example:

/** TIM1 Flags */
typedef enum
{
TIM1_FLAG_UPDATE = ((u16)0x0001),
TIM1_FLAG_CC1 = ((u16)0x0002),
TIM1_FLAG_CC2 = ((u16)0x0004),
TIM1_FLAG_CC3 = ((u16)0x0008),
TIM1_FLAG_CC4 = ((u16)0x0010),
TIM1_FLAG_COM = ((u16)0x0020),
TIM1_FLAG_TRIGGER = ((u16)0x0040),
TIM1_FLAG_BREAK = ((u16)0x0080),
TIM1_FLAG_CC1OF = ((u16)0x0200),
TIM1_FLAG_CC2OF = ((u16)0x0400),
TIM1_FLAG_CC3OF = ((u16)0x0800),
TIM1_FLAG_CC4OF = ((u16)0x1000)
}TIM1_FLAG_TypeDef;

So if I try to use "#pragma ET(char)" in my code, I get the following error at compile-time:

*** ERROR C260 IN LINE 442 OF stm8s_tim1.h : Enumeration values must be less than 256 when using ENUMTYPE(BYTE).

Is my only alternative to use "#define" definitions for my states and store them in a char data type? This seems less elegant.

Replies
Post Information Post
+1
0
-1
January 7, 2010 - 10:42am
Guest

JQ wrote:
Is my only alternative to use "#define" definitions for my states and store them in a char data type? This seems less elegant.

You can use 'enum' to define the constants without defining a type
enum
{
     ONE =   1,
     TWO =   2,
     THREE = 3
}

u8 var = ONE;
+1
0
-1
January 7, 2010 - 2:24pm
Raisonance Support Team

Happy new year Andy!

The C standard mandates that enums are of type "int".

The ST library uses enums to create some predefined values, which is not a good thing to do in the 8/16 bits world: The compiler has to perform lots of tricks to optimize code, and it is always best to let it do the optimizations the way it prefers to.

So: Using #defines instead of enums is a good thing.

Notes:
- ST has released a set of "optimized STM8S examples" on their Web site. These examples show how to perform the library functions without calling any actual functions (library ismostly present there to define the peripheral registers, not for the functions). Using their examples as a base will help you gain lots of code.
- In the 32-bits world it is definitively better to use enums than #defines, as the compiler does not have to play trick in order to meet constraints between the C standard and the limited CPU resources. In this world code readability is much more important than its efficiency.

Regards,
Bruno