Hi,
I'm facing a weird problem resulting from the following code:
while((!twi_tx_done) && timeout--);
The main file and the MPU9150.c which includes this command are attached main-2016-02-16.c mpu9150.c. Normally the program should exit this loop whenever a TWI transmit is complete and the handler sets twi_tx_done = true. Timeout is just setting a limit on the wait time for TWI to get completed.
I'll explain below how I tested the program, but the result is that: when the loop is empty, it ALWAYS exits with the timeout = 0 (which is much longer than what a normal twi_tx is). Checking inside the handler and after the while loop show that twi_tx_done is SET TO TRUE by the handler, but the while loop can not detect it!
I tried to add a toggling pin inside the while loop to see when it exits and re-enter the loop. Interestingly, with this single command added, it started working and now it exits at the same time the handler sets the twi_tx_done = true !
As toggling an IO consumes power, I though that maybe adding any command inside the loop can make it work. But, when changed the command to nrf_gpio_pin_clear(x) it again exits at the timeout!
Test setup: Here is the path to the functions including the while loop:
mpu_init->mpu9150_init (main.c) -> mpu9150_write_single (mpu9150.c)
mpu_init->mpu9150_config (main.c) -> mpu9150_write_burst (mpu9150.c)
In the figures below:
CH1-Yellow: SCL = TWI Clock
CH3-Purple: PIN0 = Toggles whenever the handler sets twi_tx_done = true + whenever the while loop exits with a timeout
CH2-Green: PIN3 = Toggles if(twi_tx_done) after the while loop
CH4-Pink: PIN 20 = Toggles whenever program enters write_single & write_burst function + exactly before the while loop
For toggling inside the while loop, PIN20 is used.
CASE 1: When an IO toggle is used inside the loop, the program works as expected
Enlarged trace, showing that no timeout occurred
CASE 2: The toggle command is NOT used in the write_single function (left half) and the program exits at timeout (vs) Right half, which toggling is used