I2C's sequence of commands problems with HMC6352

Hi, I have seen some codes about I2C , such as the sample code of HMC6352:

void loop() {

 Wire.beginTransmission(slaveAddress);
  Wire.write("A");              // The "Get Data" command
  Wire.endTransmission();
  delay(10);  
   Wire.requestFrom(slaveAddress, 2);        // Request the 2 byte heading (MSB comes first)
  i = 0;
  while(Wire.available() && i < 2)
  { 
    headingData[i] = Wire.read();
    i++;
  }
  headingValue = headingData[0]*256 + headingData[1];
   
  delay(500);
}

Why Wire.endTransmission() locate before Wire.requestFrom(slaveAddress, 2)? If I request two bytes of data from the slave , doesn’t it means I haven’t finish the transmission yet?

Another question is about the HMC6352, I read its datasheet for a while, and I don’t know how to read its
RAM register properly,because it doesn’t give any RAM map,some of it seems “write only” to set different modes ,some of it should store the data if I am right… and when should I read the RAM ?
Because from the upon code ,I don’t see any requirement about reading RAM.

The beginTransmission does not start a I2C transmission, it starts writing data. The endTransmission closes writing data.

To write data : beginTransmission --- write (one ore more) --- endTransmission

To read data : requestFrom

The requestFrom can be followed by available and/or read and/or readBytes.

Okay,I see. And do you know the hcm6352's standby mode? " This is the factory default mode. The HMC6352 waits for master device commands or change in operational mode. Receiving an “A” command (get data) will make the HMC6352 perform a measurement of sensors (magnetometers), compute the compensated magnetometer and heading data, and wait for the next read or command. No new measurements are done until another “A” command is sent. This mode is useful to get data on demand or at random intervals as long as the application can withstand the time delay in getting the data. " Dose this mode need RAM?

Sorry, I don't understand the sensor. I took a look at the datasheet, but it is confusing. You need a working library or example code.

Hii.. Wire.endTransmission() will locate before Wire.requestFrom(slaveAddress, 2) because this Wire.endTransmission() function is used to end the write operation of the command byte only onto the bus.

Also, the function Wire.requestFrom(slaveAddress, 2) when requests the two bytes of data has an operation inbuilt for starting the transmission as a request and hence, further Wire.available() checks the availability of requested bytes on I2C bus.

I've gone through the datasheet . And, yes there is no separate requirement to read out the RAM in your code as when you are sending the command 'A', you are itself reading data from RAM. In standby mode also, when you are using it you are reading data from RAM only.

batman_d408, for the 'normal' Arduino board like the Arduino Uno, the Wire.endTransmission() does the start, the address, the writing of the data bytes, and the stop. The Wire.requestFrom() does everything, and stores the received data into a buffer. The Wire.available() and Wire.read() only use that buffer of the Wire library, the actual Wire transmission over the I2C bus has already ended when the Wire.requestFrom() is finished.

You mean the endTransmission() start transmission,right? so how do I know if the transmission failed , the ACK belongs which part? device address ? register address,or writing of the data bytes? or it doesn't matter, we just care the transmission success or not?

The Wire.endTransmission() returns an error and the Wire.requestFrom() returns the number of received bytes. You can use those return values to detect a problem.

Can Wire.endTransmission() show the error comes from device address or register address?

Most I2C chips accept data without complaining, either a good register address with good values or bad data. But that is up to the manufacturer. If for example the register address would be too high, it will probably roll over inside the I2C chip. Some chips have features in undocumented register addresses which are only used during calibration at the factory. Some chips have undocumented features that the manufacturer does not reveil.

Here are the error numbers: https://www.arduino.cc/en/Reference/WireEndTransmission

If the I2C hardware is okay, and there is no other Master, then an error is most likely that the Slave did not acknowledge its address.