Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

value of uint8_t array suddenly changed and sizeof value is not correct

Hi

I was creating a function to send i2c data trough sensor that I use. But when I double check the communication, there is a part that doesn't works properly. So as you can see, below is the function :

int ch201_write_byte(ch201_device *dev_ptr, uint16_t mem_addr, uint8_t data_value) {
	uint8_t message[2] = { sizeof(data_value), data_value };
	int ch_err = i2c_block_write(CH201_I2C_ADDR_PROG, mem_addr, sizeof(message), message);
	return ch_err;
}

That code used many times, but when I check all the time, the result goes wrong when it goes to int ch_err = i2c_block_write...

I create you an that putting watchdog for checking all of the variables that happens.

Below is the first time it is executed, so data_value is 0x00, but message array goes wrong, it is 0x75, 0x6c. It should be 0x00, 0x01. CMIIW 

Another example is in the image below. The data_value is 0x10. But when you check message, it is 0x00 and 0x00. It should be 0x01,0x10. CMIIW

because of that, the sensor doesn't works properly, since the sensor works with SoC system, so I need to double-check all of the communication process.

Anybody experience in this issue? Thanks.

  • Hi,

    You are right, sizeof a uint8 is always 1. This could be caused by a stack overflow, some other part of the code in an interrupt for instance that overwrites and corrupts this memory, or similar. It could also be just a issue with debugging, for instance if you are debugging optimized code things might look strange. A bit more information about your code and how you debug would be needed in order to say more precisely what the cause is here.

  • Hi, thanks for responding Einar.

    Anyway, I got the solution. My senior engineer told me to expand it and implement it one by one. The way to implement this array is like the code below.

    int ch201_write_byte(ch201_device *dev_ptr, uint16_t mem_addr, uint8_t data_value) {
    	uint8_t message[2];
    	message[0] = sizeof(data_value);
    	message[1] = data_value;
    	int ch_err = i2c_block_write(CH201_I2C_ADDR_PROG, mem_addr, sizeof(message), message);
    	return ch_err;
    }

    TBH, I don't know why I have to do that, but at least it works. Hehe.

    Thanks.

Related