static int sensor_board_bmm150_read(i2c_device dev) { /* Structure used for read the mag xyz data*/ struct bmm050_mag_data_s16_t data; /* result of communication results*/ s32 com_rslt = ERROR; bmm150_dev = dev; bool sign; uint32_t conv_val; /* * Initialize driver data, feed Bosch driver with functions needed to access I2C bus. */ com_rslt = bmm050_init(&bmm150); /* * For initialization it is required to set the state of * the sensor as "NORMAL MODE" * data acquisition/read/write is possible in this mode * by using the below API able to set the power mode as NORMAL */ com_rslt += bmm050_set_functional_state(BMM050_NORMAL_MODE); /* * Set data rate */ com_rslt += bmm050_set_data_rate(BMM050_DATA_RATE_30HZ); /* Wait for data ready */ OS_DELAY(1000 / 30 + 1); /* accessing the bmm050_mdata parameter by using data*/ com_rslt += bmm050_read_mag_data_XYZ(&data); /* print raw values */ printf(NEWLINE "Compensated magnetometer data (raw 16 bit) - x: %04X, y: %04X, z: %04X", (uint16_t) data.datax, (uint16_t) data.datay, (uint16_t) data.dataz); /* convert and print values in uT (microtesla unit) */ printf(NEWLINE "Compensated magnetometer data - "); convert_xy_value(data.datax, &sign, &conv_val); printf("x: %s%lu.%02lu uT, ", (sign ? "-" : ""), conv_val / 100, conv_val % 100); convert_xy_value(data.datay, &sign, &conv_val); printf("y: %s%lu.%02lu uT, ", (sign ? "-" : ""), conv_val / 100, conv_val % 100); convert_z_value(data.dataz, &sign, &conv_val); printf("z: %s%lu.%02lu uT", (sign ? "-" : ""), conv_val / 100, conv_val % 100); /* For de-initialization it is required to set the mode of * the sensor as "SUSPEND" * the SUSPEND mode set from the register 0x4B bit BMM050_INIT_VALUE should be disabled * by using the below API able to set the power mode as SUSPEND */ com_rslt += bmm050_set_functional_state(BMM050_SUSPEND_MODE); return com_rslt; }
OSStatus bmm050_sensor_deinit(void) { OSStatus err = kUnknownErr; s32 com_rslt = BMM050_ERROR; err = MicoI2cFinalize(&bmm050_i2c_device); require_noerr_action( err, exit, bmm050_user_log("BMM050_ERROR: MicoI2cFinalize err = %d.", err)); /*---------------------------------------------------------------------------* *********************** START DE-INITIALIZATION ***************************** *--------------------------------------------------------------------------*/ /* For de-initialization it is required to set the mode of * the sensor as "SUSPEND" * the SUSPEND mode set from the register 0x4B bit BMM050_INIT_VALUE should be disabled * by using the below API able to set the power mode as SUSPEND*/ /* Set the power mode as SUSPEND*/ com_rslt = bmm050_set_functional_state(BMM050_SUSPEND_MODE); /*--------------------------------------------------------------------------* *********************** END DE-INITIALIZATION ************************** *---------------------------------------------------------------------------*/ if(0 == com_rslt){ err = kNoErr; } exit: return err; }
OSStatus bmm050_sensor_init(void) { OSStatus err = kUnknownErr; /* Variable used to get the data rate*/ u8 v_data_rate_u8 = BMM050_INIT_VALUE; /* Variable used to set the data rate*/ u8 v_data_rate_value_u8 = BMM050_INIT_VALUE; s32 com_rslt = BMM050_ERROR; // result of communication results // u8 v_stand_by_time_u8 = BME280_INIT_VALUE; // The variable used to assign the standby time // I2C init MicoI2cFinalize(&bmm050_i2c_device); // in case error err = MicoI2cInitialize(&bmm050_i2c_device); require_noerr_action( err, exit, bmm050_user_log("BMM050_ERROR: MicoI2cInitialize err = %d.", err) ); if( false == MicoI2cProbeDevice(&bmm050_i2c_device, 5) ){ bmm050_user_log("BMM050_ERROR: no i2c device found!"); err = kNotFoundErr; goto exit; } // sensor init /*********************** START INITIALIZATION ************************/ /* Based on the user need configure I2C or SPI interface. * It is example code to explain how to use the bme280 API*/ #ifdef BMM050_API BMM050_I2C_routine(); com_rslt = bmm050_init(&bmm050_t); com_rslt += bmm050_set_functional_state(BMM050_NORMAL_MODE); /*------------------------------------------------------------------------* ************************* START GET and SET FUNCTIONS DATA **************** *---------------------------------------------------------------------------*/ /* This API used to Write the data rate of the sensor, input value have to be given data rate value set from the register 0x4C bit 3 to 5*/ v_data_rate_value_u8 = BMM050_DATA_RATE_30HZ;/* set data rate of 30Hz*/ com_rslt += bmm050_set_data_rate(v_data_rate_value_u8); /* This API used to read back the written value of data rate*/ com_rslt += bmm050_get_data_rate(&v_data_rate_u8); /*-----------------------------------------------------------------* ************************* END GET and SET FUNCTIONS **************** *-------------------------------------------------------------------*/ if(com_rslt < 0){ bmm050_user_log("BMM050_ERROR: bme280 sensor init failed!"); err = kNotInitializedErr; goto exit; } /************************* END INITIALIZATION *************************/ #endif return kNoErr; exit: return err; }
/* This function is an example for reading sensor data * \param: None * \return: communication result */ s32 bmm050_data_readout_template(void) { /* Structure used for read the mag xyz data*/ struct bmm050_mag_data_s16_t data; /* Structure used for read the mag xyz data with 32 bit output*/ struct bmm050_mag_s32_data_t data_s32; /* Structure used for read the mag xyz data with float output*/ struct bmm050_mag_data_float_t data_float; /* Variable used to get the data rate*/ u8 v_data_rate_u8 = BMM050_INIT_VALUE; /* Variable used to set the data rate*/ u8 v_data_rate_value_u8 = BMM050_INIT_VALUE; /* result of communication results*/ s32 com_rslt = 0; /*---------------------------------------------------------------------------* *********************** START INITIALIZATION ************************ *--------------------------------------------------------------------------*/ /* Based on the user need configure I2C or SPI interface. * It is sample code to explain how to use the bmm050 API*/ #ifdef BMM050_API BMM050_I2C_routine(); /*BMM050_SPI_routine(); */ #endif /*--------------------------------------------------------------------------* * This function used to assign the value/reference of * the following parameters * I2C address * Bus Write * Bus read * company_id *-------------------------------------------------------------------------*/ com_rslt = bmm050_init(&bmm050_t); /* For initialization it is required to set the mode of * the sensor as "NORMAL" * but before set the mode needs to configure the power control bit * in the register 0x4B bit BMM050_INIT_VALUE should be enabled * This bit is enabled by calling bmm050_init function * For the Normal data acquisition/read/write is possible in this mode * by using the below API able to set the power mode as NORMAL*/ /* Set the power mode as NORMAL*/ com_rslt += bmm050_set_functional_state(BMM050_NORMAL_MODE); /*--------------------------------------------------------------------------* ************************* END INITIALIZATION ************************* *---------------------------------------------------------------------------*/ /*------------------------------------------------------------------------* ************************* START GET and SET FUNCTIONS DATA **************** *---------------------------------------------------------------------------*/ /* This API used to Write the data rate of the sensor, input value have to be given data rate value set from the register 0x4C bit 3 to 5*/ v_data_rate_value_u8 = BMM050_DATA_RATE_30HZ;/* set data rate of 30Hz*/ com_rslt += bmm050_set_data_rate(v_data_rate_value_u8); /* This API used to read back the written value of data rate*/ com_rslt += bmm050_get_data_rate(&v_data_rate_u8); /*-----------------------------------------------------------------* ************************* END GET and SET FUNCTIONS **************** *-------------------------------------------------------------------*/ /*------------------------------------------------------------------* ************************* START READ SENSOR DATA(X,Y and Z axis) ******** *------------------------------------------------------------------*/ /* accessing the bmm050_mdata parameter by using data*/ com_rslt += bmm050_read_mag_data_XYZ(&data);/* Reads the mag x y z data*/ /* accessing the bmm050_mdata_float parameter by using data_float*/ com_rslt += bmm050_read_mag_data_XYZ_float(&data_float);/* Reads mag xyz data output as 32bit value*/ /* accessing the bmm050_mdata_s32 parameter by using data_s32*/ com_rslt += bmm050_read_mag_data_XYZ_s32(&data_s32);/* Reads mag xyz data output as float value*/ /*--------------------------------------------------------------------* ************************* END READ SENSOR DATA(X,Y and Z axis) ************ *-------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------* ************************* START DE-INITIALIZATION *********************** *-------------------------------------------------------------------------*/ /* For de-initialization it is required to set the mode of * the sensor as "SUSPEND" * the SUSPEND mode set from the register 0x4B bit BMM050_INIT_VALUE should be disabled * by using the below API able to set the power mode as SUSPEND*/ /* Set the power mode as SUSPEND*/ com_rslt += bmm050_set_functional_state(BMM050_SUSPEND_MODE); /*---------------------------------------------------------------------* ************************* END DE-INITIALIZATION ********************** *---------------------------------------------------------------------*/ return com_rslt; }