bool hmc5883lSelfTest()
{
bool testStatus = TRUE;
int16_t mxp, myp, mzp; // positive magnetometer measurements
int16_t mxn, myn, mzn; // negative magnetometer measurements
struct
{
uint8_t configA;
uint8_t configB;
uint8_t mode;
} regSave;
// Save register values
if (i2cdevRead(I2Cx, devAddr, HMC5883L_RA_CONFIG_A, sizeof(regSave), (uint8_t *)®Save) == FALSE)
{
// TODO: error handling
return FALSE;
}
// Set gain (sensitivity)
hmc5883lSetGain(HMC5883L_ST_GAIN);
// Write CONFIG_A register and do positive test
i2cdevWriteByte(I2Cx, devAddr, HMC5883L_RA_CONFIG_A,
(HMC5883L_AVERAGING_1 << (HMC5883L_CRA_AVERAGE_BIT - HMC5883L_CRA_AVERAGE_LENGTH + 1)) |
(HMC5883L_RATE_15 << (HMC5883L_CRA_RATE_BIT - HMC5883L_CRA_RATE_LENGTH + 1)) |
(HMC5883L_BIAS_POSITIVE << (HMC5883L_CRA_BIAS_BIT - HMC5883L_CRA_BIAS_LENGTH + 1)));
hmc5883lSetMode(HMC5883L_MODE_SINGLE);
vTaskDelay(M2T(HMC5883L_ST_DELAY_MS));
hmc5883lGetHeading(&mxp, &myp, &mzp);
// Write CONFIG_A register and do negative test
i2cdevWriteByte(I2Cx, devAddr, HMC5883L_RA_CONFIG_A,
(HMC5883L_AVERAGING_1 << (HMC5883L_CRA_AVERAGE_BIT - HMC5883L_CRA_AVERAGE_LENGTH + 1)) |
(HMC5883L_RATE_15 << (HMC5883L_CRA_RATE_BIT - HMC5883L_CRA_RATE_LENGTH + 1)) |
(HMC5883L_BIAS_NEGATIVE << (HMC5883L_CRA_BIAS_BIT - HMC5883L_CRA_BIAS_LENGTH + 1)));
hmc5883lSetMode(HMC5883L_MODE_SINGLE);
vTaskDelay(M2T(HMC5883L_ST_DELAY_MS));
hmc5883lGetHeading(&mxn, &myn, &mzn);
if (hmc5883lEvaluateSelfTest(HMC5883L_ST_X_MIN, HMC5883L_ST_X_MAX, mxp, "pos X") &&
hmc5883lEvaluateSelfTest(HMC5883L_ST_Y_MIN, HMC5883L_ST_Y_MAX, myp, "pos Y") &&
hmc5883lEvaluateSelfTest(HMC5883L_ST_Z_MIN, HMC5883L_ST_Z_MAX, mzp, "pos Z") &&
hmc5883lEvaluateSelfTest(-HMC5883L_ST_X_MAX, -HMC5883L_ST_X_MIN, mxn, "neg X") &&
hmc5883lEvaluateSelfTest(-HMC5883L_ST_Y_MAX, -HMC5883L_ST_Y_MIN, myn, "neg Y") &&
hmc5883lEvaluateSelfTest(-HMC5883L_ST_Z_MAX, -HMC5883L_ST_Z_MIN, mzn, "neg Z"))
{
DEBUG_PRINT("Self test [OK].n");
}
else
{
testStatus = FALSE;
}
// Restore registers
if (i2cdevWrite(I2Cx, devAddr, HMC5883L_RA_CONFIG_A, sizeof(regSave), (uint8_t *)®Save) == FALSE)
{
// TODO: error handling
return FALSE;
}
return testStatus;
} |