Bit field operation issue.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Bit field operation issue.

523 Views
charudattaingal
Contributor IV

Hi, I am using MC9S12ZVL family controller and CW IDE, facing problem with bit field operations. When I do some anding operation on particular bit it’s changing others bit value too.

 

Bit field structure:-

typedef struct _c_test_bit

{

  uint16_t test_bit_1 : 1;

  uint16_t test_bit_2 : 1;

  uint16_t test_bit_3 : 1;

  uint16_t test_bit_4 : 1; 

  uint16_t test_bit_5 : 1;

  uint16_t test_bit_6 : 1;

  uint16_t test_bit_7 : 1;

  uint16_t test_bit_8 : 1; 

  uint16_t test_bit_9 : 1;

  uint16_t test_bit_10 : 1;

  uint16_t test_bit_11 : 1;

  uint16_t test_bit_12 : 1; 

  uint16_t test_bit_13 : 1;

  uint16_t test_bit_14 : 1;

  uint16_t test_bit_15 : 1;

  uint16_t test_bit_16 : 1; 

  uint16_t test_bit_17 : 1;

  uint16_t test_bit_18 : 1;

  uint16_t unused0 : 6;

  uint16_t unused1 : 8;

  uint16_t unused2 : 8;

  uint16_t unused3 : 8;

  uint16_t unused4 : 8;

  uint16_t unused5 : 8;

} _c_test_bit;

 

Declaration:-

typedef union _c_test_bufTag

{

  uint8_t _c[8];

  _c_test_bit test_var;

} _c_test_buf;

 

_c_test_buf test_ver_1;

 

Anding operation:-

 

test_ver_1.test_var.test_bit_16 = ((uint8_t) (((uint8_t) (a)) & 0x01));  /* Note:- a can be 0 or 1 based on set or clear operation */

 

This operation is causing to change other bitfields values.

 

Below compiler options are used:-

CFLAGS += -nostdinc

CFLAGS += -nosyspath

CFLAGS += -w illpragmas

CFLAGS += -w possible

CFLAGS += -w extended

CFLAGS += -w extracomma

CFLAGS += -w emptydecl

CFLAGS += -w structclass

CFLAGS += -w notinlined

CFLAGS += -model small

CFLAGS += -bfield_lsbit_first

CFLAGS += -bfield_reduce_type

CFLAGS += -opt level=0

CFLAGS += -opt space

CFLAGS += -requireprotos

CFLAGS += -Cpp_Exceptions off

CFLAGS += -char unsigned

CFLAGS += -msgstyle parseable

CFLAGS += -sym full

 

Thanks and Regards,

Charudatta

Labels (1)
0 Kudos
1 Reply

361 Views
lama
NXP TechSupport
NXP TechSupport

Hi,

I have checked you issue and not able to reproduce the issue.

I have used following approach:

All used variables are global.

I am checking the value of the bitfield members in the “Variables” windov and there is no issue.

Are you able to do some simple test like presented to be sure there is nothing else in the SW which can influence the bitfield.

_c_test_buf test_ver_1;

static uint8_t a=0;

//----------------------------------------------------------------------------------

void CPMU_Init( void )

{

  CPMUPROT    = 0x26u; // Disable protection of clock configuration registers

  // PLL setup 16 MHz BUSCLK from 4 MHz

  CPMUSYNR = 0x03;

  CPMUREFDIV = 0x40;

  CPMUPOSTDIV = 0x00;

  CPMUOSC_OSCE = 1;

  while( CPMUIFLG_UPOSC == 0 ) {};

  do

    {

      CPMUCLKS = 0B10000001;   // COP clk is OSCCLK

    }

  while( CPMUCLKS != 0B10000001);

}

//----------------------------------------------------------------------------------

void bitfield_problem_solution_ctm_issue(void)

{

  a++;

  /* Note:- a can be 0 or 1 based on set or clear operation */

  test_ver_1.test_var.test_bit_16 = ((uint8_t) (((uint8_t) (a)) & 0x01));

}

//---------------------------------------------------------------------------------- 

void main( void )

{

  UWORD i,j;

 

  //=== CLOCK and COP INIT =======

  CPMU_Init();                    //BUSCLK for 16MHz from OSCCLK 4MHz

  for(;;)

   {

     bitfield_problem_solution_ctm_issue();

   }

}

//----------------------------------------------------------------------------------

Best regards,

Ladislav

0 Kudos