hw/drivers/sensors/bmx160/src/bmm150_compensate.c
int16_t bmm150_compensate_z(const struct bmm150_trim_regs *trim, uint16_t data_rhall, int16_t mag_data_z)
{
    int32_t retval;
    int16_t process_comp_z0;
    int32_t process_comp_z1;
    int32_t process_comp_z2;
    int32_t process_comp_z3;
    int16_t process_comp_z4;

    if (mag_data_z == BMM150_ZAXIS_HALL_OVERFLOW_ADCVAL)
        return  BMM150_OVERFLOW_OUTPUT;
    
    if ((trim->dig_z1 == 0) || (trim->dig_z2 == 0))
        return  BMM150_OVERFLOW_OUTPUT;

    if ((data_rhall == 0) || (trim->dig_xyz1 == 0))
        return  BMM150_OVERFLOW_OUTPUT;

    /*Processing compensation equations*/
    process_comp_z0 = ((int16_t)data_rhall) - ((int16_t) trim->dig_xyz1);
    process_comp_z1 = (((int32_t)trim->dig_z3) * ((int32_t)(process_comp_z0))) / 4;
    process_comp_z2 = (((int32_t)(mag_data_z - trim->dig_z4)) * 32768);
    process_comp_z3 = ((int32_t)trim->dig_z1) * (((int16_t)data_rhall) * 2);
    process_comp_z4 = (int16_t)((process_comp_z3 + (32768)) / 65536);
    retval = ((process_comp_z2 - process_comp_z1) / (trim->dig_z2 + process_comp_z4));

    /* saturate result to +/- 2 micro-tesla */
    if (retval > BMM150_POSITIVE_SATURATION_Z)
    {
        retval = BMM150_POSITIVE_SATURATION_Z;
    }
    else if (retval < BMM150_NEGATIVE_SATURATION_Z)
    {
        retval = BMM150_NEGATIVE_SATURATION_Z;
    }

    /* Conversion of LSB to micro-tesla*/
    retval = retval / 16;

    return (int16_t)retval;
}
/*!
 * @brief This internal API is used to obtain the compensated
 * magnetometer Z axis data(micro-tesla) in int16_t.
 */
@brief This internal API is used to obtain the compensated
magnetometer Z axis data(micro-tesla) in int16_t.
@brief This internal API is used to obtain the compensated magnetometer Z axis data ( micro - tesla ) in int16_t .
int16_t bmm150_compensate_z(const struct bmm150_trim_regs *trim, uint16_t data_rhall, int16_t mag_data_z)
{
    int32_t retval;
    int16_t process_comp_z0;
    int32_t process_comp_z1;
    int32_t process_comp_z2;
    int32_t process_comp_z3;
    int16_t process_comp_z4;
    if (mag_data_z == BMM150_ZAXIS_HALL_OVERFLOW_ADCVAL)
        return  BMM150_OVERFLOW_OUTPUT;
    if ((trim->dig_z1 == 0) || (trim->dig_z2 == 0))
        return  BMM150_OVERFLOW_OUTPUT;
    if ((data_rhall == 0) || (trim->dig_xyz1 == 0))
        return  BMM150_OVERFLOW_OUTPUT;
    process_comp_z0 = ((int16_t)data_rhall) - ((int16_t) trim->dig_xyz1);
    process_comp_z1 = (((int32_t)trim->dig_z3) * ((int32_t)(process_comp_z0))) / 4;
    process_comp_z2 = (((int32_t)(mag_data_z - trim->dig_z4)) * 32768);
    process_comp_z3 = ((int32_t)trim->dig_z1) * (((int16_t)data_rhall) * 2);
    process_comp_z4 = (int16_t)((process_comp_z3 + (32768)) / 65536);
    retval = ((process_comp_z2 - process_comp_z1) / (trim->dig_z2 + process_comp_z4));
    if (retval > BMM150_POSITIVE_SATURATION_Z)
    {
        retval = BMM150_POSITIVE_SATURATION_Z;
    }
    else if (retval < BMM150_NEGATIVE_SATURATION_Z)
    {
        retval = BMM150_NEGATIVE_SATURATION_Z;
    }
    retval = retval / 16;
    return (int16_t)retval;
}
int16_t bmm150_compensate_z ( const struct bmm150_trim_regs * trim , uint16_t data_rhall , int16_t mag_data_z ) { int32_t retval ; int16_t process_comp_z0 ; int32_t process_comp_z1 ; int32_t process_comp_z2 ; int32_t process_comp_z3 ; int16_t process_comp_z4 ; if ( mag_data_z == BMM150_ZAXIS_HALL_OVERFLOW_ADCVAL ) return BMM150_OVERFLOW_OUTPUT ; if ( ( trim -> dig_z1 == 0 ) || ( trim -> dig_z2 == 0 ) ) return BMM150_OVERFLOW_OUTPUT ; if ( ( data_rhall == 0 ) || ( trim -> dig_xyz1 == 0 ) ) return BMM150_OVERFLOW_OUTPUT ; process_comp_z0 = ( ( int16_t ) data_rhall ) - ( ( int16_t ) trim -> dig_xyz1 ) ; process_comp_z1 = ( ( ( int32_t ) trim -> dig_z3 ) * ( ( int32_t ) ( process_comp_z0 ) ) ) / 4 ; process_comp_z2 = ( ( ( int32_t ) ( mag_data_z - trim -> dig_z4 ) ) * 32768 ) ; process_comp_z3 = ( ( int32_t ) trim -> dig_z1 ) * ( ( ( int16_t ) data_rhall ) * 2 ) ; process_comp_z4 = ( int16_t ) ( ( process_comp_z3 + ( 32768 ) ) / 65536 ) ; retval = ( ( process_comp_z2 - process_comp_z1 ) / ( trim -> dig_z2 + process_comp_z4 ) ) ; if ( retval > BMM150_POSITIVE_SATURATION_Z ) { retval = BMM150_POSITIVE_SATURATION_Z ; } else if ( retval < BMM150_NEGATIVE_SATURATION_Z ) { retval = BMM150_NEGATIVE_SATURATION_Z ; } retval = retval / 16 ; return ( int16_t ) retval ; }
@brief This internal API is used to obtain the compensated
 magnetometer Z axis data(micro-tesla) in int16_t.
@brief This internal API is used to obtain the compensated magnetometer Z axis data ( micro - tesla ) in int16_t .
/*Processing compensation equations*/ /* saturate result to +/- 2 micro-tesla */ /* Conversion of LSB to micro-tesla*/