This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

warning: implicit truncation from 'int' to bitfield....

Dear Forum,

I have searched all of Keil's website, manuals and the internet about this issue and have found nothing that can eliminate this compiler warning in my "C" source code.

I am getting a warning throughout my code showing a symbol that appears as an explanation point inside a yellow triangle in the left shaded source column that when I pass the cursor over reads: "warning: implicit truncation from 'int' to bitfield changes value from 3 to -1". The related statements are: GPIOE->GPIO_MODER.P03 = GPIO_OUTPUT; // PE03 - SCOPE1 GPIOE->GPIO_OTYPER.P03 = GPIO_PUSHPULL; GPIOE->GPIO_OSPEEDR.P03 = GPIO_HIGH;
where the third line is flagged with this warning.

Although the compiler does generate the proper assembly statements to correctly implement my statement, these warnings are all over, but only flagged on certain statements. I haven't gotten these warnings in prior Keil versions. It appears to only occur, in this example, if GPIO_FAST or GPIO_HIGH are used in which for these two bits, the higher bit being set causes the warning as if the 2-bit field is "signed".

using FAST: "warning: implicit truncation from 'int' to bitfield changes value from 2 to -2"
using HIGH: "warning: implicit truncation from 'int' to bitfield changes value from 3 to -1"

The compiler setting "Enum Container always int" is checked. I also have defined my "u32" as "typedef unsigned long u32;" and been using this with Keil since before the "uint32_t" came out, plus it's shorter.

I have replaced STM's difficult to read, use and prone to bugs register definitions that use "#define xxxx" with typedefs of bitfields, structs and enums so that the compiler verifies all read and write terms into the registers. This particular warning is related to my GPIO register definitions (in order as written):

typedef enum // gpio speeds { GPIO_LOW = 0, // low speed (2MHz) (reset state) GPIO_MED = 1, // medium speed (25MHz) GPIO_FAST = 2, // fast speed (50MHz) GPIO_HIGH = 3 // high speed (100MHz) } gpio_ospeed_e;

typedef struct // port speed selects { gpio_ospeed_e P00 : 2; // portx.0 gpio_ospeed_e P01 : 2; // portx.1 gpio_ospeed_e P02 : 2; // portx.2 gpio_ospeed_e P03 : 2; // portx.3 gpio_ospeed_e P04 : 2; // portx.4 gpio_ospeed_e P05 : 2; // portx.5 gpio_ospeed_e P06 : 2; // portx.6 gpio_ospeed_e P07 : 2; // portx.7 gpio_ospeed_e P08 : 2; // portx.8 gpio_ospeed_e P09 : 2; // portx.9 gpio_ospeed_e P10 : 2; // portx.10 gpio_ospeed_e P11 : 2; // portx.11 gpio_ospeed_e P12 : 2; // portx.12 gpio_ospeed_e P13 : 2; // portx.13 gpio_ospeed_e P14 : 2; // portx.14 gpio_ospeed_e P15 : 2; // portx.15 } gpio_ospeed_b;

typedef struct // STM32FF429 GPIO REGISTERS { __IO gpio_mode_b GPIO_MODER; // MODE [PA = 0xA8000000] // [PB = 0x00000280] // [PC-PD = 0] __IO gpio_otype_b GPIO_OTYPER; // OUTPUT TYPE [0] __IO gpio_ospeed_b GPIO_OSPEEDR; // OUTPUT SPEED [0] __IO gpio_pupd_b GPIO_PUPDR; // PULL-UP/DN [PA = 0x64000000] // [PB = 0x00000100] // [PC-PD = 0] __I gpio_dr_u GPIO_IDR; // INPUT DATA (R) [0] __O gpio_dr_u GPIO_ODR; // OUTPUT DATA [0] __O gpio_bsrr_b GPIO_BSRR; // BIT SET/CLEAR (W) [0] __IO u32 GPIO_LCKR; // LOCK [0] __O gpio_afrl_b GPIO_AFRL; // ALT FUNCTION LOW [0] __O gpio_afrh_b GPIO_AFRH; // ALT FUNCTION HIGH [0] } gpio_reg_t;

#define GPIOE_BASE (0x40021000) #define GPIOE ((gpio_reg_t *)GPIOE_BASE)

I appreciate any feedback on how force the compiler to ignore or eliminate this warning.

Sincerely,
Robbie