int bma020_write_ee(unsigned char addr, unsigned char data) { int comres; if (p_bma020==0) /* check pointers */ return E_BMA020_NULL_PTR; if (p_bma020->delay_msec == 0) return E_BMA020_NULL_PTR; comres = bma020_set_ee_w( BMA020_EE_W_ON ); addr|=0x20; /* add eeprom address offset to image address if not applied */ comres += bma020_write_reg(addr, &data, 1 ); p_bma020->delay_msec( BMA020_EE_W_DELAY ); comres += bma020_set_ee_w( BMA020_EE_W_OFF); return comres; }
int bma020_calibrate(bma020acc_t orientation, int *tries) { unsigned short offset_x, offset_y, offset_z; unsigned short old_offset_x, old_offset_y, old_offset_z; unsigned short changed_offset_x, changed_offset_y, changed_offset_z; int need_calibration=0, min_max_ok=0; int ltries; int retry = 30; bma020acc_t min,max,avg; printk("[%s] +\n", __func__); bma020_set_range(BMA020_RANGE_2G); bma020_set_bandwidth(BMA020_BW_25HZ); bma020_set_ee_w(1); bma020_get_offset(0, &offset_x); bma020_get_offset(1, &offset_y); bma020_get_offset(2, &offset_z); old_offset_x = offset_x; old_offset_y = offset_y; old_offset_z = offset_z; ltries = *tries; printk("[%s] old offset_x = %d, offset_y = %d, offset_z = %d\n", __func__, old_offset_x, offset_y, offset_z); orientation.x = 0; orientation.y = 0; orientation.z = 1; do { bma020_read_accel_avg(10, &min, &max, &avg); /* read acceleration data min, max, avg */ min_max_ok = bma020_verify_min_max(min, max, avg); if(!min_max_ok) { retry--; if(retry <= 0) return (-1); } /* check if calibration is needed */ if (min_max_ok) need_calibration = bma020_calc_new_offset(orientation, avg, &offset_x, &offset_y, &offset_z); if (*tries==0) /*number of maximum tries reached? */ break; if (need_calibration) { /* when needed calibration is updated in image */ printk("[%s] need calibration. tries = %d. changed offset x = %d, y = %d, z = %d\n", __func__, *tries, offset_x, offset_y, offset_z); (*tries)--; bma020_set_offset(0, offset_x); bma020_set_offset(1, offset_y); bma020_set_offset(2, offset_z); bma020_pause(20); } printk("\n"); } while (need_calibration || !min_max_ok); if (*tries>0 && *tries < ltries) { printk("[%s] eeprom is updated. new offset x=%d, y=%d, z=%d\n", __func__, offset_x, offset_y, offset_z); if (old_offset_x!= offset_x) bma020_set_offset_eeprom(0, offset_x); if (old_offset_y!= offset_y) bma020_set_offset_eeprom(1,offset_y); if (old_offset_z!= offset_z) bma020_set_offset_eeprom(2, offset_z); } printk("[%s] tries = %d\n", __func__, *tries); bma020_set_ee_w(0); bma020_pause(20); *tries = ltries - *tries; printk("[%s] -\n", __func__); return !need_calibration; }