/** * @brief BMA150 ioctl control entry point * * @param hal Address of an initialized sensor hardware descriptor. * @param cmd Command to execute * @param arg Argument for command (varies) * @return bool true if the call succeeds, else false is returned. */ static bool bma150_ioctl(sensor_t *sensor, sensor_command_t cmd, void *arg) { sensor_hal_t *const hal = sensor->hal; sensor_data_t sample = {.scaled = true}; switch (cmd) { case SENSOR_SET_STATE: return bma150_set_state(hal, *((sensor_state_t *)arg)); case SENSOR_SET_RANGE: return bma150_set_range(hal, (uint16_t)*((int *)arg)); case SENSOR_SET_BANDWIDTH: return bma150_set_bandwidth(hal, (uint16_t)*((int *)arg)); case SENSOR_ENABLE_EVENT: return bma150_event(sensor, *((sensor_event_t *)arg), 0, true); case SENSOR_DISABLE_EVENT: return bma150_event(sensor, *((sensor_event_t *)arg), 0, false); case SENSOR_SET_THRESHOLD: return bma150_set_threshold(hal, (sensor_threshold_desc_t *)arg); case SENSOR_GET_THRESHOLD: return bma150_get_threshold(hal, (sensor_threshold_desc_t *)arg); case SENSOR_READ_VECTOR: if (bma150_get_accel(hal, &sample)) { vector3_t *const pvec = (vector3_t *)arg; pvec->x = sample.axis.x; pvec->y = sample.axis.y; pvec->z = sample.axis.z; return true; } else { return false; } default: sensor->err = SENSOR_ERR_UNSUPPORTED; return false; } }
int tcc_sensor_set_bandwidth(struct i2c_client *client,char bw) { return bma150_set_bandwidth(client,bw); }
int bma150_calibration(struct bmasensor_data *sensor_data) { struct i2c_client *client = sensor_data->i2cClient; unsigned short offset_x, offset_y, offset_z; unsigned short old_offset_x, old_offset_y, old_offset_z; int need_calibration = 0, min_max_ok = 0; int ltries = 50; tcc_sensor_accel_t min, max, avg; bma150_set_range(client,BMA150_RANGE_2G); bma150_set_bandwidth(client,BMA150_BW_25HZ); bma150_set_ee_w(client,BMA150_EE_W_ON); bma150_get_offset(client,0, &offset_x); bma150_get_offset(client,1, &offset_y); bma150_get_offset(client,2, &offset_z); old_offset_x = offset_x; old_offset_y = offset_y; old_offset_z = offset_z; do { /* read acceleration data min, max, avg */ //printk(" bma150_calibrate remain try count %d \n",(ltries)); bma150_read_accel_avg(client,10, &min, &max, &avg); min_max_ok = bma150_verify_min_max(min, max, avg); /* check if calibration is needed */ if (min_max_ok) need_calibration = bma150_calc_new_offset(avg, &offset_x, &offset_y, &offset_z); if (ltries == 0) /*number of maximum tries reached? */ break; if (need_calibration) { //printk(" new offset %d %d %d \n",offset_x,offset_y,offset_z); /* when needed calibration is updated in image */ (ltries)--; bma150_set_offset(client,0, offset_x); bma150_set_offset(client,1, offset_y); bma150_set_offset(client,2, offset_z); msleep(10); } } while (need_calibration || !min_max_ok); //printk(" need_calibration %d min_max_ok %d ltries %d \n",need_calibration,min_max_ok,(ltries)); /* if (ltries > 0) { if (old_offset_x != offset_x) bma150_set_offset_eeprom(0, offset_x); if (old_offset_y != offset_y) bma150_set_offset_eeprom(1, offset_y); if (old_offset_z != offset_z) bma150_set_offset_eeprom(2, offset_z); } */ bma150_set_ee_w(client,BMA150_EE_W_OFF); msleep(24); return !need_calibration; }