Same code works and it doesn't

Hello everybody,
I am having a weird problem with a program in arduino. I'm trying to make a program that uses the MPU-9150 (SparkFun 9DoF IMU Breakout - ICM-20948 (Qwiic) - SEN-15335 - SparkFun Electronics).
The first thing I'm trying to do is to ask the MPU its name and identify the connection.
I have wired it up and I have connection with the MPU and I'm using the Wire library.
I wrote this function:

void identify()
{
  Wire.beginTransmission(DEVICE_ADDRESS);
  Wire.write(WHO_AM_I);
  Wire.endTransmission();

  Wire.requestFrom(DEVICE_ADDRESS, 1);

  uint8_t test;
  while (Wire.available()) {
    test = Wire.read();
  }

  Serial.println(test, HEX);
}

This works fine but only in the setup region. If I call it from the loop it doesn't work.

Then I tried to write a more generic function to read and write from the chip (like I2Cdevlib) but it doesn't work at all. I tried exactly the same code as "identify" above but it doesn't work!!!

void readFrom(uint8_t Address, short length, uint8_t data[])
{
  Wire.beginTransmission(DEVICE_ADDRESS);
  Wire.write(WHO_AM_I);
  Wire.endTransmission();
  
  Wire.requestFrom(DEVICE_ADDRESS, 1);

  uint8_t test;
  while (Wire.available()) {
    test = Wire.read();
  }

  Serial.println(test, HEX);
}

I'm pretty new to arduino and maybe I'm missing something but I don't understand why the first code works but the second does not.
I moved the code form readFrom(), exactly as is, in the setup region and it works!
Am I missing something here? Thank you in advance.

I'm using Arduino Uno with 1.0.1 IDE from Fedora.

edit: Forgot to mention that If I call both function then none works

identify();
  delay(1000);
  readFrom(0,0, &data);

Can you explain what you mean when you say that things don't work ?
What does happen when you try ?

I'm pretty new to arduino and maybe I'm missing something but I don't understand why the first code works but the second does not.

So how are we supposed to tell from what you posted. Two almost identical functions.
We need to see it in context therefore you need to post ALL your code.

UKHeliBob:
Can you explain what you mean when you say that things don't work ?
What does happen when you try ?

The supposed return from MPU-9150 from register 0x75 (WHO_AM_I) is 0x68.
When I use the identify function it returns 0x68. When I use the readFrom function it returns 0xf8.

Grumpy_Mike:

I'm pretty new to arduino and maybe I'm missing something but I don't understand why the first code works but the second does not.

So how are we supposed to tell from what you posted. Two almost identical functions.
We need to see it in context therefore you need to post ALL your code.

My problem is that the two functions (at least their body) is perfectly identical (copy-paste) but they return different things.
My full test code is the following:

#include "Arduino.h"
#include <Wire.h>

#define DEVICE_ADDRESS  0x69
#define WHO_AM_I        0x75

void identify();
void readFrom(uint8_t Address, short length, uint8_t data);

void setup()
{
  Serial.begin(9600);
  delay(100);
  uint8_t data;

  identify();
//  readFrom(0,0, &data);
}

void loop()
{
}

void identify()
{
  Wire.beginTransmission(DEVICE_ADDRESS);
  Wire.write(WHO_AM_I);
  Wire.endTransmission();

  Wire.requestFrom(DEVICE_ADDRESS, 1);

  uint8_t test;
  while (Wire.available()) {
    test = Wire.read();
  }

  Serial.println(test, HEX);
}

void readFrom(uint8_t Address, short length, uint8_t data[])
{
  Wire.beginTransmission(DEVICE_ADDRESS);
  Wire.write(WHO_AM_I);
  Wire.endTransmission();

  Wire.requestFrom(DEVICE_ADDRESS, 1);

  uint8_t test;
  while (Wire.available()) {
    test = Wire.read();
  }

  Serial.println(test, HEX);
}

As is right now the result is 0x68.
If I comment out the "identify();" and uncomment "readFrom(0,0, &data);" the result is 0xF8.
If I uncomment both these lines the result is 0xF8 0xF8.

It's the first time I see such behavior from a device so it is hard for me to explain it too.
Thank you for your time.

  Wire.requestFrom(DEVICE_ADDRESS, 1);

  uint8_t test;
  while (Wire.available()) {
    test = Wire.read();
  }

While I2C is fast, you seem to be under the mistaken impression that it is instantaneous. When you get over that, I predict you'll have better results.

Instituting callbacks, to be called when the data IS available is preferred. Not discarding results, in case the slave has a better idea how much data to return is preferred, too.

PaulS:
While I2C is fast, you seem to be under the mistaken impression that it is instantaneous. When you get over that, I predict you'll have better results.
Instituting callbacks, to be called when the data IS available is preferred. Not discarding results, in case the slave has a better idea how much data to return is preferred, too.

I understand that it is not instantaneous and I tried using delay functions as well. But the results remain the same.

My biggest question is why I have different results using the same code?

I tried one more thing. I changed the prototype of readFrom()
from

void readFrom(uint8_t Address, short length, uint8_t data);

to

void readFrom(uint8_t Address, short length);

and it worked fine, like identify().
Is there something I don't understand about programming arduino or is it a bug or something?

I figured it out.
The problem was with the way I was trying to access the IS. It needed to be reset and then get it out of sleep before I could access the WHO_AM_I register.

I still don't know why there was some false returns (in linux, in windows I had no return at all).

Thank you for your time.