cancel
Showing results for 
Search instead for 
Did you mean: 

LSM303AGR does not seem to store hard iron compensation values I send during configuration

HIngv.1
Associate II

I've gone through the documentation a few times and I'm not sure what I'm missing. I'm using an STM32L4 to control the lsm303agr through an i2c connection. When I run debug and probe the SCL/SDA lines on my oscilliscope, the messages look good, and from what I am seeing I am getting a slave acknowledge at the end of each transmit.

I first noticed when I was collecting data to calculate hard iron compensation values that after I thought I had sent calibration data to the registers that my collected data looked exactly the same. I made a function to read out the OFFSET registers, and the lsm303agr sends back that all 6 offset registers are 0x00 (seen both in debug and oscilliscope)

Am I getting the procedure to load the compensation data to the registers incorrect?

Magnetometer config registers:

//Magnetometer configuration
	//CFG_REG_A_M
	msg[0] = CFG_REG_A_M;
	msg[1] = 0x88;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);
 
	//CFG_REG_B_M
	msg[0] = CFG_REG_B_M;
	msg[1] = 0x02;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);
 
	//CFG_REG_C_M
	msg[0] = CFG_REG_C_M;
	msg[1] = 0x10;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);

sending hard iron values to registers:

	//Magnetometer hard-iron compensation values will go here
	msg[0] = OFFSET_X_REG_L_M;
	msg[1] = 0x7F;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);
 
	msg[0] = OFFSET_X_REG_H_M;
	msg[1] = 0xFF;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);
 
	msg[0] = OFFSET_Y_REG_L_M;
	msg[1] = 0xFF;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);
 
	msg[0] = OFFSET_Y_REG_H_M;
	msg[1] = 0xFF;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);
 
	msg[0] = OFFSET_Z_REG_L_M;
	msg[1] = 0xD7;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);
 
	msg[0] = OFFSET_Z_REG_H_M;
	msg[1] = 0xFF;
	HAL_I2C_Master_Transmit(&hi2c1, ACC_ADDR, msg, 2, HAL_MAX_DELAY);

and then code when I try to read the values from the registers:

void spitIron(void){
	uint8_t reg;
	uint8_t data;
 
	reg = OFFSET_X_REG_L_M;
	HAL_I2C_Master_Transmit(&hi2c1, MAG_ADDR, &reg, 1, HAL_MAX_DELAY);
	HAL_I2C_Master_Receive(&hi2c1, MAG_ADDR, &data, 1, HAL_MAX_DELAY);
	printf("%s %x \n", "X Low:", data);
 
	reg = OFFSET_X_REG_H_M;
	HAL_I2C_Master_Transmit(&hi2c1, MAG_ADDR, &reg, 1, HAL_MAX_DELAY);
	HAL_I2C_Master_Receive(&hi2c1, MAG_ADDR, &data, 1, HAL_MAX_DELAY);
	printf("%s %x \n", "X High:", data);
 
	reg = OFFSET_Y_REG_L_M;
	HAL_I2C_Master_Transmit(&hi2c1, MAG_ADDR, &reg, 1, HAL_MAX_DELAY);
	HAL_I2C_Master_Receive(&hi2c1, MAG_ADDR, &data, 1, HAL_MAX_DELAY);
	printf("%s %x \n", "Y Low:", data);
 
	reg = OFFSET_Y_REG_H_M;
	HAL_I2C_Master_Transmit(&hi2c1, MAG_ADDR, &reg, 1, HAL_MAX_DELAY);
	HAL_I2C_Master_Receive(&hi2c1, MAG_ADDR, &data, 1, HAL_MAX_DELAY);
	printf("%s %x \n", "Y High:", data);
 
	reg = OFFSET_Z_REG_L_M;
	HAL_I2C_Master_Transmit(&hi2c1, MAG_ADDR, &reg, 1, HAL_MAX_DELAY);
	HAL_I2C_Master_Receive(&hi2c1, MAG_ADDR, &data, 1, HAL_MAX_DELAY);
	printf("%s %x \n", "Z Low:", data);
 
	reg = OFFSET_Z_REG_H_M;
	HAL_I2C_Master_Transmit(&hi2c1, MAG_ADDR, &reg, 1, HAL_MAX_DELAY);
	HAL_I2C_Master_Receive(&hi2c1, MAG_ADDR, &data, 1, HAL_MAX_DELAY);
	printf("%s %x \n", "Z High:", data);
 
}//end spitIron

1 REPLY 1
Federica Bossi
ST Employee

Hi @HIngv.1​ ,

When you set the magnetometer configuration and hard-iron compensation you use ACC_ADDR instead of MAG_ADDR. Please, try to use MAG_ADDR as described in Table 25 below and let me know if you fix the problem.


_legacyfs_online_stmicro_images_0693W00000bhrmNQAQ.png

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.