Ejemplo n.º 1
0
OSStatus bmg160_sensor_init(void)
{
  OSStatus err = kUnknownErr;
  /* variable used for read the gyro bandwidth data*/
  u8 v_gyro_value_u8 = BMG160_INIT_VALUE;
  /* variable used for set the gyro bandwidth data*/
  u8 v_bw_u8 = BMG160_INIT_VALUE;
  s32 com_rslt = BMG160_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(&bmg160_i2c_device);   // in case error
  err = MicoI2cInitialize(&bmg160_i2c_device);
  require_noerr_action( err, exit, bmg160_user_log("BMG160_ERROR: MicoI2cInitialize err = %d.", err) );
  if( false == MicoI2cProbeDevice(&bmg160_i2c_device, 5) ){
    bmg160_user_log("BMG160_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 BMG160_API
  BMG160_I2C_routine();
  com_rslt = bmg160_init(&bmg160);
  com_rslt += bmg160_set_power_mode(BMG160_MODE_NORMAL);
  /*------------------------------------------------------------------------*
  ************************* START GET and SET FUNCTIONS DATA ***************
  *--------------------------------------------------------------------------*/
  /* This API used to Write the bandwidth of the gyro sensor
  input value have to be give 0x10 bit BMG160_INIT_VALUE to 3
  The bandwidth set from the register */
  v_bw_u8 = C_BMG160_BW_230HZ_U8X;/* set gyro bandwidth of 230Hz*/
  com_rslt += bmg160_set_bw(v_bw_u8);

  /* This API used to read back the written value of bandwidth for gyro*/
  com_rslt += bmg160_get_bw(&v_gyro_value_u8);
  /*---------------------------------------------------------------------*
  ************************* END GET and SET FUNCTIONS ********************
  *----------------------------------------------------------------------*/
  if(com_rslt < 0){
    bmg160_user_log("BMG160_ERROR: bme280 sensor init failed!");
    err = kNotInitializedErr;
    goto exit;
  }
  /************************* END INITIALIZATION *************************/
#endif
  return kNoErr;
  
exit:
  return err;
}
Ejemplo n.º 2
0
/* This function is an example for reading sensor data
 *	\param: None
 *	\return: communication result
 */
s32 bmg160_data_readout_template(void)
{
	/* Gyro */
	/* variable used for read the sensor data*/
	s16	v_gyro_datax_s16, v_gyro_datay_s16, v_gyro_dataz_s16 = BMG160_INIT_VALUE;
	/* structure used for read the sensor data - xyz*/
	struct bmg160_data_t data_gyro;
	/* structure used for read the sensor data - xyz and interrupt status*/
	struct bmg160_data_t gyro_xyzi_data;
	/* variable used for read the gyro bandwidth data*/
	u8	v_gyro_value_u8 = BMG160_INIT_VALUE;
	/* variable used for set the gyro bandwidth data*/
	u8 v_bw_u8 = BMG160_INIT_VALUE;
	/* result of communication results*/
	s32 com_rslt = ERROR;

/*-------------------------------------------------------------------------*
 *********************** START INITIALIZATION ***********************
 *-------------------------------------------------------------------------*/
 /*	Based on the user need configure I2C or SPI interface.
  *	It is example code to explain how to use the bmg160 API*/
  #ifdef BMG160_API
  	I2C_routine();
	/*SPI_routine(); */
	#endif
/*--------------------------------------------------------------------------*
 *  This function used to assign the value/reference of
 *	the following parameters
 *	Gyro I2C address
 *	Bus Write
 *	Bus read
 *	Gyro Chip id
 *----------------------------------------------------------------------------*/
	com_rslt = bmg160_init(&bmg160);
/*----------------------------------------------------------------------------*/
/*	For initialization it is required to set the mode of the sensor as "NORMAL"
 *	data acquisition/read/write is possible in this mode
 *	by using the below API able to set the power mode as NORMAL
 *	NORMAL mode set from the register 0x11 and 0x12
 *	While sensor in the NORMAL mode idle time of at least 2us(micro seconds)
 *	is required to write/read operations
 *	0x11 -> bit 5,7 -> set value as BMG160_INIT_VALUE
 *	0x12 -> bit 6,7 -> set value as BMG160_INIT_VALUE
 *	Note:
 *		If the sensor is in the fast power up mode idle time of least
 *		450us(micro seconds) required for write/read operations
 */

/*-------------------------------------------------------------------------*/
	/* Set the gyro power mode as NORMAL*/
	com_rslt += bmg160_set_power_mode(BMG160_MODE_NORMAL);
/*--------------------------------------------------------------------------*
************************* END INITIALIZATION ******************************
*--------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*
************************* START GET and SET FUNCTIONS DATA ***************
*--------------------------------------------------------------------------*/
/* This API used to Write the bandwidth of the gyro sensor
	input value have to be give 0x10 bit BMG160_INIT_VALUE to 3
	The bandwidth set from the register */
	v_bw_u8 = C_BMG160_BW_230HZ_U8X;/* set gyro bandwidth of 230Hz*/
	com_rslt += bmg160_set_bw(v_bw_u8);

/* This API used to read back the written value of bandwidth for gyro*/
	com_rslt += bmg160_get_bw(&v_gyro_value_u8);
/*---------------------------------------------------------------------*
************************* END GET and SET FUNCTIONS ********************
*----------------------------------------------------------------------*/
/*---------------------------------------------------------------------*
************************* START READ SENSOR DATA(X,Y and Z axis) *********
*-------------------------------------------------------------------------*/
/******************* Read Gyro data xyz**********************/
	com_rslt += bmg160_get_data_X(&v_gyro_datax_s16);/* Read the gyro X data*/

	com_rslt += bmg160_get_data_Y(&v_gyro_datay_s16);/* Read the gyro Y data*/

	com_rslt += bmg160_get_data_Z(&v_gyro_dataz_s16);/* Read the gyro Z data*/

/* accessing the  bmg160_data_t parameter by using data_gyro*/
	com_rslt += bmg160_get_data_XYZ(&data_gyro);/* Read the gyro XYZ data*/

/* accessing the bmg160_data_t parameter by using gyro_xyzi_data*/
/* Read the gyro XYZ data and interrupt status*/
	com_rslt += bmg160_get_data_XYZI(&gyro_xyzi_data);
/*--------------------------------------------------------------------------
************************* 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 "DEEPSUSPEND"
 *	the device reaches the lowest power consumption only
 *	interface selection is kept alive
 *	No data acquisition is performed
 *	The DEEPSUSPEND mode set from the register 0x11 bit 5
 *	by using the below API able to set the power mode as DEEPSUSPEND
 *	For the read/ write operation it is required to provide least 450us
 *	micro second delay*/

	com_rslt += bmg160_set_power_mode(BMG160_MODE_DEEPSUSPEND);

/*--------------------------------------------------------------------------*
*********************** END DE-INITIALIZATION **************************
*---------------------------------------------------------------------------*/
return com_rslt;
}
Ejemplo n.º 3
0
static int bmg160_set_mode(struct bmg160_p *data, unsigned char mode)
{
	int ret = 0;
	unsigned char buf1, buf2, buf3;
	unsigned char autosleepduration;
	unsigned char v_bw_u8r;

	ret = bmg160_i2c_read(data->client, BMG160_MODE_LPM1_ADDR, &buf1);
	ret += bmg160_i2c_read(data->client, BMG160_MODE_LPM2_ADDR, &buf2);

	switch (mode) {
	case BMG160_MODE_NORMAL:
		buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 0);
		buf2 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 0);
		buf3 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 0);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM1_ADDR, buf1);
		mdelay(1);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM2_ADDR, buf3);
		break;
	case BMG160_MODE_DEEPSUSPEND:
		buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 1);
		buf2 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 0);
		buf3 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 0);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM1_ADDR, buf1);
		mdelay(1);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM2_ADDR, buf3);
		break;
	case BMG160_MODE_SUSPEND:
		buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 4);
		buf2 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 0);
		buf3 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 0);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM1_ADDR, buf1);
		mdelay(1);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM2_ADDR, buf3);
		break;
	case BMG160_MODE_FASTPOWERUP:
		buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 4);
		buf2 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 1);
		buf3 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 0);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM1_ADDR, buf1);
		mdelay(1);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM2_ADDR, buf3);
		break;
	case BMG160_MODE_ADVANCEDPOWERSAVING:
		/* Configuring the proper settings for auto
		sleep duration */
		bmg160_get_bw(data, &v_bw_u8r);
		bmg160_get_autosleepdur(data, &autosleepduration);
		bmg160_set_autosleepdur(data, autosleepduration, v_bw_u8r);
		ret += bmg160_i2c_read(data->client,
				BMG160_MODE_LPM2_ADDR, &buf2);
		/* Configuring the advanced power saving mode*/
		buf1 = BMG160_SET_BITSLICE(buf1, BMG160_MODE_LPM1, 0);
		buf2 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_FAST_POWERUP, 0);
		buf3 = BMG160_SET_BITSLICE(buf2,
			BMG160_MODE_LPM2_ADDR_ADV_POWERSAVING, 1);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM1_ADDR, buf1);
		mdelay(1);
		ret += bmg160_i2c_write(data->client,
				BMG160_MODE_LPM2_ADDR, buf3);
		break;
	default:
		ret = -EINVAL;
		break;
	}

	return ret;
}