Generally, all we encounter is a small-end scenario. A typical X86 processor is a small-end scenario.
However, many powerpc processors can be configured in the big-end mode or small-end mode. Therefore, the previous code is often run well, to the new board, or board configuration
If a change occurs, the result is incorrect and debugging is difficult. Because it is not considered wrong at all.
The following is an issue that we have detected after a long time in the code.
Another function:
I2c_write (..., unsigned char * reg ,...)
The reg type here is an unsigned char pointer, representing the Register address of the i2c device.
In the previous code, a cTest with the int type will be declared before passing the parameter, and then the following call will be made:
I2c_write (..., (unsigned char *) & cTest ,...);
On the previous simba Board, there was no problem with this. Simba is a board of the MPC8572 on the powerpc platform. It was configured as a small-end model.
Yes. In this case, it seems that there will be no problems.
For example
Int cTest = 2;
That is, cTest = 0x00000002, the passed Pointer Points to the address where 0x02 is located, that is, the lowest cTest address.
Now it is changed to tembo, which is a P4080 board on the powerpc platform and configured as the big-end mode.
Still yes
Int cTest = 2;
That is, cTest = 0x00000002. Does the pointer still point to the address where 0x02 is located?
Apparently not!
We can see (unsigned char *) & cTest this operation, this C Standard certainly has a provision, will get a pointer to the lowest address of & cTest unsigned char type.
At this time, the priority bit of & cTest is the highest valid bit, and the content is 0x00. Therefore, no correct result will be obtained!
It took a long time to solve such a small problem. Ultimately, it is caused by programming habits.
It is too casual to write code, regardless of the platform or parameter type. In fact, it is best to include signed and unsigned In the Parameter definition.
It cannot be assumed that the compiler considers the char type to be signed or not, although it is signed in most cases.
There is also strict parameter passing. It is best to define what type of parameters are needed. Do not make any assumptions about the compiler!
From Jing chaotian's column