// bme280_sensor_init OSStatus bme280_sensor_init(void) { OSStatus err = kUnknownErr; s32 com_rslt = BME280_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(&user_i2c_device); // in case error err = MicoI2cInitialize(&user_i2c_device); require_noerr_action( err, exit, bme280_user_log("BME280_ERROR: MicoI2cInitialize err = %d.", err) ); if( false == MicoI2cProbeDevice(&user_i2c_device, 5) ){ bme280_user_log("BME280_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 BME280_API I2C_routine(); com_rslt = bme280_init(&bme280); com_rslt += bme280_set_power_mode(BME280_NORMAL_MODE); com_rslt += bme280_set_oversamp_humidity(BME280_OVERSAMP_1X); com_rslt += bme280_set_oversamp_pressure(BME280_OVERSAMP_2X); com_rslt += bme280_set_oversamp_temperature(BME280_OVERSAMP_4X); /************************* START GET and SET FUNCTIONS DATA ****************/ //com_rslt += bme280_set_standby_durn(BME280_STANDBY_TIME_1_MS); //com_rslt += bme280_get_standby_durn(&v_stand_by_time_u8); /************************** END GET and SET FUNCTIONS **************/ if(com_rslt < 0){ bme280_user_log("BME280_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 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; }
///////////////// // main routine ///////////////// void main (void) { int8_t ret1, ret2; int16_t accX1, accY1, accZ1; int16_t accX2, accY2, accZ2; char buf[100]; uint8_t count=0, len=0; ///////////////// // init peripherals ///////////////// // disable interrupts DISABLE_INTERRUPTS; // switch to 16MHz (default is 2MHz) CLK.CKDIVR.byte = 0x00; // set default option bytes to assert bootloader is running flash_OPT_default(); // init timer TIM3 for sleep and timeout (required by I2C) tim3_init(); // init timer TIM4 for 1ms clock with interrupts tim4_init(); // init I2C bus i2c_init(); // init and reset LCD display lcd_init(); // init pins for UART1 Rx(=PA4) and Tx(=PA5) gpio_init(&PORT_A, PIN_4, INPUT_PULLUP_NOEXINT); gpio_init(&PORT_A, PIN_5, OUTPUT_PUSHPULL_FAST); // init UART1 to high speed (connected to PC on muBoard) uart1_init(230400L); // init LEDs on muBoard for visual feedback GPIO_SET(PORT_H,PIN_2|PIN_3, 1); gpio_init(&PORT_H, PIN_2|PIN_3, OUTPUT_PUSHPULL_FAST); // enable interrupts ENABLE_INTERRUPTS; // init I2C routine pointers I2C_routine(); // initialize sensors do { bno055.dev_addr = BNO055_I2C_ADDR1; ret1 = bno055_init(&bno055); ret1 |= bno055_set_power_mode(POWER_MODE_NORMAL); ret1 |= bno055_set_operation_mode(OPERATION_MODE_AMG); } while (ret1 && USE_I2C_ADDR1); do { bno055.dev_addr = BNO055_I2C_ADDR2; ret2 = bno055_init(&bno055); ret2 |= bno055_set_power_mode(POWER_MODE_NORMAL); ret2 |= bno055_set_operation_mode(OPERATION_MODE_AMG); } while (ret2 && USE_I2C_ADDR2); ///////////////// // main loop ///////////////// while (1) { // every 1ms do if (g_flagClock) { g_flagClock = 0; // every 10ms do if (g_clock > 10) { g_clock = 0; // just to be sure accX1 = accY1 = accZ1 = ret1 = 0; accX2 = accY2 = accZ2 = ret2 = 0; // read data from sensor 1 #if USE_I2C_ADDR1 bno055.dev_addr = BNO055_I2C_ADDR1; ret1 = bno055_read_accel_x(&accX1); ret1 |= bno055_read_accel_y(&accY1); ret1 |= bno055_read_accel_z(&accZ1); if (ret1 != 0) { accX1 = accY1 = accZ1 = 0; } #endif // USE_I2C_ADDR1 // read data from sensor 2 #if USE_I2C_ADDR2 bno055.dev_addr = BNO055_I2C_ADDR2; ret2 = bno055_read_accel_x(&accX2); ret2 |= bno055_read_accel_y(&accY2); ret2 |= bno055_read_accel_z(&accZ2); if (ret2 != 0) { accX2 = accY2 = accZ2 = 0; } #endif // USE_I2C_ADDR2 // send data to PC via UART1. Use SW FIFO for background operation len = 0; buf[len++] = (uint8_t)(accX1 >> 8); // x1-acc (MSB first) buf[len++] = (uint8_t) accX1; buf[len++] = (uint8_t)(accY1 >> 8); // y1-acc (MSB first) buf[len++] = (uint8_t) accY1; buf[len++] = (uint8_t)(accZ1 >> 8); // z1-acc (MSB first) buf[len++] = (uint8_t) accZ1; buf[len++] = (uint8_t)(accX2 >> 8); // x2-acc (MSB first) buf[len++] = (uint8_t) accX2; buf[len++] = (uint8_t)(accY2 >> 8); // y2-acc (MSB first) buf[len++] = (uint8_t) accY2; buf[len++] = (uint8_t)(accZ2 >> 8); // z2-acc (MSB first) buf[len++] = (uint8_t) accZ2; uart1_send_buf(len, buf); // indicate I2C status via red LED (on=ok) GPIO_SET(PORT_H,PIN_3, ret1|ret2); // show life beat via green LED if (++count > 20) { count = 0; GPIO_TOGGLE(PORT_H,PIN_2); // print to LCD sprintf(buf, "%02d %03d %03d %03d", (int) ret1, (int) accX1, (int) accY1, (int) accZ1); lcd_print(1, 1, buf); sprintf(buf, "%02d %03d %03d %03d", (int) ret2, (int) accX2, (int) accY2, (int) accZ2); lcd_print(2, 1, buf); } } // loop 10ms } // loop 1ms } // main loop
/* This function is an example for reading sensor data * \param: None * \return: communication result */ s32 bme280_data_readout_template(void) { /* The variable used to assign the standby time*/ u8 v_stand_by_time_u8 = BME280_INIT_VALUE; /* The variable used to read uncompensated temperature*/ s32 v_data_uncomp_tem_s32 = BME280_INIT_VALUE; /* The variable used to read uncompensated pressure*/ s32 v_data_uncomp_pres_s32 = BME280_INIT_VALUE; /* The variable used to read uncompensated pressure*/ s32 v_data_uncomp_hum_s32 = BME280_INIT_VALUE; /* The variable used to read real temperature*/ s32 v_actual_temp_s32 = BME280_INIT_VALUE; /* The variable used to read real pressure*/ u32 v_actual_press_u32 = BME280_INIT_VALUE; /* The variable used to read real humidity*/ u32 v_actual_humity_u32 = BME280_INIT_VALUE; /* result of communication results*/ s32 com_rslt = BME280_ERROR; /*********************** 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 BME280_API I2C_routine(); /*SPI_routine(); */ #endif /*--------------------------------------------------------------------------* * This function used to assign the value/reference of * the following parameters * I2C address * Bus Write * Bus read * Chip id *-------------------------------------------------------------------------*/ com_rslt = bme280_init(&bme280); /* 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*/ /* Set the power mode as NORMAL*/ com_rslt += bme280_set_power_mode(BME280_NORMAL_MODE); /* For reading the pressure, humidity and temperature data it is required to * set the OSS setting of humidity, pressure and temperature * The "BME280_CTRLHUM_REG_OSRSH" register sets the humidity * data acquisition options of the device. * changes to this registers only become effective after a write operation to * "BME280_CTRLMEAS_REG" register. * In the code automated reading and writing of "BME280_CTRLHUM_REG_OSRSH" * register first set the "BME280_CTRLHUM_REG_OSRSH" and then read and write * the "BME280_CTRLMEAS_REG" register in the function*/ com_rslt += bme280_set_oversamp_humidity(BME280_OVERSAMP_1X); /* set the pressure oversampling*/ com_rslt += bme280_set_oversamp_pressure(BME280_OVERSAMP_2X); /* set the temperature oversampling*/ com_rslt += bme280_set_oversamp_temperature(BME280_OVERSAMP_4X); /*--------------------------------------------------------------------------*/ /*------------------------------------------------------------------------* ************************* START GET and SET FUNCTIONS DATA **************** *---------------------------------------------------------------------------*/ /* This API used to Write the standby time of the sensor input * value have to be given * Normal mode comprises an automated perpetual cycling between an (active) * Measurement period and an (inactive) standby period. * The standby time is determined by the contents of the register t_sb. * Standby time can be set using BME280_STANDBYTIME_125_MS. * Usage Hint : bme280_set_standbydur(BME280_STANDBYTIME_125_MS)*/ com_rslt += bme280_set_standby_durn(BME280_STANDBY_TIME_1_MS); /* This API used to read back the written value of standby time*/ com_rslt += bme280_get_standby_durn(&v_stand_by_time_u8); /*-----------------------------------------------------------------* ************************* END GET and SET FUNCTIONS **************** *------------------------------------------------------------------*/ /************************* END INITIALIZATION *************************/ /*------------------------------------------------------------------* ************ START READ UNCOMPENSATED PRESSURE, TEMPERATURE AND HUMIDITY DATA ******** *---------------------------------------------------------------------*/ /* API is used to read the uncompensated temperature*/ com_rslt += bme280_read_uncomp_temperature(&v_data_uncomp_tem_s32); /* API is used to read the uncompensated pressure*/ com_rslt += bme280_read_uncomp_pressure(&v_data_uncomp_pres_s32); /* API is used to read the uncompensated humidity*/ com_rslt += bme280_read_uncomp_humidity(&v_data_uncomp_hum_s32); /* API is used to read the uncompensated temperature,pressure and humidity data */ com_rslt += bme280_read_uncomp_pressure_temperature_humidity( &v_data_uncomp_tem_s32, &v_data_uncomp_pres_s32, &v_data_uncomp_hum_s32); /*--------------------------------------------------------------------* ************ END READ UNCOMPENSATED PRESSURE AND TEMPERATURE******** *-------------------------------------------------------------------------*/ /*------------------------------------------------------------------* ************ START READ TRUE PRESSURE, TEMPERATURE AND HUMIDITY DATA ******** *---------------------------------------------------------------------*/ /* API is used to read the true temperature*/ /* Input value as uncompensated temperature and output format*/ com_rslt += bme280_compensate_temperature_int32(v_data_uncomp_tem_s32); /* API is used to read the true pressure*/ /* Input value as uncompensated pressure */ com_rslt += bme280_compensate_pressure_int32(v_data_uncomp_pres_s32); /* API is used to read the true humidity*/ /* Input value as uncompensated humidity and output format*/ com_rslt += bme280_compensate_humidity_int32(v_data_uncomp_hum_s32); /* API is used to read the true temperature, humidity and pressure*/ com_rslt += bme280_read_pressure_temperature_humidity( &v_actual_press_u32, &v_actual_temp_s32, &v_actual_humity_u32); /*--------------------------------------------------------------------* ************ END READ TRUE PRESSURE, TEMPERATURE AND HUMIDITY ******** *-------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------* ************************* START DE-INITIALIZATION *********************** *-------------------------------------------------------------------------*/ /* For de-initialization it is required to set the mode of * the sensor as "SLEEP" * the device reaches the lowest power consumption only * In SLEEP mode no measurements are performed * All registers are accessible * by using the below API able to set the power mode as SLEEP*/ /* Set the power mode as SLEEP*/ com_rslt += bme280_set_power_mode(BME280_SLEEP_MODE); /*---------------------------------------------------------------------* ************************* END DE-INITIALIZATION ********************** *---------------------------------------------------------------------*/ return com_rslt; }
/* This function is an example for reading sensor data * \param: None * \return: communication result */ s32 bma2x2_data_readout_template(void) { /*Local variables for reading accel x, y and z data*/ s16 accel_x_s16, accel_y_s16, accel_z_s16 = BMA2x2_INIT_VALUE; /* bma2x2acc_data structure used to read accel xyz data*/ struct bma2x2_accel_data sample_xyz; /* bma2x2acc_data_temp structure used to read accel xyz and temperature data*/ struct bma2x2_accel_data_temp sample_xyzt; /* Local variable used to assign the bandwidth value*/ u8 bw_value_u8 = BMA2x2_INIT_VALUE; /* Local variable used to set the bandwidth value*/ u8 banwid = BMA2x2_INIT_VALUE; /* status of communication*/ 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 bma2x2 API*/ #ifdef BMA2x2_API I2C_routine(); /*SPI_routine(); */ #endif /*--------------------------------------------------------------------------* * This function used to assign the value/reference of * the following parameters * I2C address * Bus Write * Bus read * Chip id *-------------------------------------------------------------------------*/ com_rslt = bma2x2_init(&bma2x2); /* For initialization it is required to set the mode of * the sensor as "NORMAL" * NORMAL mode is set from the register 0x11 and 0x12 * 0x11 -> bit 5,6,7 -> set value as 0 * 0x12 -> bit 5,6 -> set value as 0 * data acquisition/read/write is possible in this mode * by using the below API able to set the power mode as NORMAL * For the Normal/standby/Low power 2 mode Idle time of at least 2us(micro seconds) * required for read/write operations*/ /* Set the power mode as NORMAL*/ com_rslt += bma2x2_set_power_mode(BMA2x2_MODE_NORMAL); /* Note: * For the Suspend/Low power1 mode Idle time of at least 450us(micro seconds) * required for read/write operations*/ /************************* END INITIALIZATION *************************/ /*------------------------------------------------------------------------* ************************* START GET and SET FUNCTIONS DATA **************** *---------------------------------------------------------------------------*/ /* This API used to Write the bandwidth of the sensor input value have to be given bandwidth is set from the register 0x10 bits from 1 to 4*/ bw_value_u8 = 0x08;/* set bandwidth of 7.81Hz*/ com_rslt += bma2x2_set_bw(bw_value_u8); /* This API used to read back the written value of bandwidth*/ com_rslt += bma2x2_get_bw(&banwid); /*-----------------------------------------------------------------* ************************* END GET and SET FUNCTIONS **************** *-------------------------------------------------------------------*/ /*------------------------------------------------------------------* ************************* START READ SENSOR DATA(X,Y and Z axis) ******** *---------------------------------------------------------------------*/ /* Read the accel X data*/ com_rslt += bma2x2_read_accel_x(&accel_x_s16); /* Read the accel Y data*/ com_rslt += bma2x2_read_accel_y(&accel_y_s16); /* Read the accel Z data*/ com_rslt += bma2x2_read_accel_z(&accel_z_s16); /* accessing the bma2x2acc_data parameter by using sample_xyz*/ /* Read the accel XYZ data*/ com_rslt += bma2x2_read_accel_xyz(&sample_xyz); /* accessing the bma2x2acc_data_temp parameter by using sample_xyzt*/ /* Read the accel XYZT data*/ com_rslt += bma2x2_read_accel_xyzt(&sample_xyzt); /*--------------------------------------------------------------------* ************************* 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 "DEEP SUSPEND" * DEEP SUSPEND mode is set from the register 0x11 * 0x11 -> bit 5 -> set value as 1 * the device reaches the lowest power consumption only * interface selection is kept alive * No data acquisition is performed * by using the below API able to set the power mode as DEEPSUSPEND*/ /* Set the power mode as DEEPSUSPEND*/ com_rslt += bma2x2_set_power_mode(BMA2x2_MODE_DEEP_SUSPEND); /*---------------------------------------------------------------------* ************************* END DE-INITIALIZATION ********************** *---------------------------------------------------------------------*/ return com_rslt; }