function with 8bit Parameters pushes 16 bit to stack but pops only 8 bit

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

function with 8bit Parameters pushes 16 bit to stack but pops only 8 bit

Jump to solution
1,283 Views
ttom
Contributor I

Hello,

I have a HCS08SG32 and want to do the following (environment: Codewarrior 6.3, hardware: demo board DEMO9S08SH32/SG32) :

 

call of function:

i2c_read (0x20, 0x30, 2, send);

I discoverd an error, because inside the function i2c_read() the values of the parameters are not equal to the call of the function.

 

where send is defined:

unsigned char send[11];

 

i2c_read is defined as:

unsigned char i2c_read (unsigned char devaddr, unsigned char addr, unsigned char len, unsigned char *data);

 

 

When I step by the debugger into the function, the parameters are pushed at the stack as 16 bits values (see attached file), but are popped as 8 bit values. As a result: the parameters inside the function are cut and have not the values like in the call (devaddr = 2, addr=0, len =0) .

If I change the datatype of the parameters to a 16 Bit value, it works.

But I want to understand, why 8 Bit parameters are pushed to stack as 16 Bit Values and wrongly popped inside the function as 8 bit.

 

I already went trough the compiler options, but did not find a setting to influence this.

 

I hope, somebody can remove the tomatos from my eyes?

Many thanks in advance

Thomas

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
974 Views
BlackNight
NXP Employee
NXP Employee

Hi Thomas,

I suggest to use the -Wpd compiler option: this option will flag any missing prototype as an error.

That way you can easily spot the programming error (hopefully).

 

Hope this helps,

BK

View solution in original post

0 Kudos
Reply
6 Replies
974 Views
CrasyCat
Specialist III

Hello

 

It looks like you forget to place a prototype of function i2c_read prior to invoking it.

 

Please preprocess the source file where the function is called and look for a prototype for function i2c_read.

 

CrasyCat

0 Kudos
Reply
974 Views
ttom
Contributor I

Hello CrazyCat,

I have now created a preprocessor output (main.pre) file, and had a look into it.

Inside this file, there is no prototype of the function i2c_reaD(), but there is the include of i2c.h. Within this file, the function i2c_reaD() is defined by the prototype.

I am not sure if you meant this.

 

Thomas

0 Kudos
Reply
974 Views
CrasyCat
Specialist III

Hello

 

   The wrong parameter passing is due to the missing prototype.

 

  Now you need to understand why the prototype is not added to the preprocessor listing.

  Check if there is any conditional compilation  directive (#if or #ifdef) that might comment out the function prototype.

 

CrasyCat

0 Kudos
Reply
975 Views
BlackNight
NXP Employee
NXP Employee

Hi Thomas,

I suggest to use the -Wpd compiler option: this option will flag any missing prototype as an error.

That way you can easily spot the programming error (hopefully).

 

Hope this helps,

BK

0 Kudos
Reply
974 Views
ttom
Contributor I

Hello all,

many thanks for your help!

It is solved now:

In my task, I want to switch the interface to a eeprom from SPI to I2C.

Just started by copy&paste the spi.c and spi.h files and rename to i2c.c and .h.

In the header file, there was the #define around the complete file

#ifndef _SPI_H_

#define _SPI_H_

....

#endif

 

And, of course, i did not rename this define into I2C...

So I included the right file in my main.c, but the prototype was not inserted because the spi.h and spi.c file is still in the project...

 

Many thanks again for your help!

 

Thomas

0 Kudos
Reply
974 Views
ttom
Contributor I

Hello CrazyCat,

thank you for your quick replay.

You are right, I did not include the header file, in which the prototype is defined.

 

Now, I included the header file, but no change in the behaviour.

 

Next, I renamed the function from i2c_read() to i2c_reaD() (in protoype, implementation of function and function call), just to avoid, that there is elsewhere another function which that name: But no change, too.

 

Now, I let create the predef.h file, to get to know what is defined (but I do not see a define, which may cause this problem):

#define __STDC__ 0#define __VERSION__ 5032#define __VERSION_STR__ "V-5.0.32"#define __MWERKS__ 1#define __BITFIELD_LSBIT_FIRST__#define __BITFIELD_MSBYTE_FIRST__#define __BITFIELD_MSWORD_FIRST__#define __BITFIELD_TYPE_SIZE_REDUCTION__#define __SMALL__#define __HC08__#define __PTR_SIZE_2__#define __HC08_ICG__#define __HCS08__#define __FLOAT_IS_IEEE32__#define __DOUBLE_IS_IEEE64__#define __LONG_DOUBLE_IS_IEEE64__#define __LONG_LONG_DOUBLE_IS_IEEE64__#define __CHAR_IS_UNSIGNED__#define __CHAR_IS_8BIT__#define __SHORT_IS_16BIT__#define __INT_IS_16BIT__#define __ENUM_IS_16BIT__#define __LONG_IS_32BIT__#define __LONG_LONG_IS_32BIT__#define __SIZE_T_IS_UINT__#define __PTRDIFF_T_IS_INT__#define __ENUM_IS_SIGNED__#define __VTAB_DELTA_IS_16BIT__#define __WCHAR_T_IS_UCHAR__#define __PTRMBR_OFFSET_IS_16BIT__#define __PLAIN_BITFIELD_IS_SIGNED__#define __ELF_OBJECT_FILE_FORMAT__#define __PRODUCT_HICROSS_PLUS__#define __HIWARE__#define __ARCHIMEDES__#define __OPTIMIZE_FOR_SIZE__#define __BIG_ENDIAN__

 I also tried with and without optimization. No changes, always the same result.

 

Thank you

Thomas

 

0 Kudos
Reply