Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2116798
  • 博文数量: 438
  • 博客积分: 3871
  • 博客等级: 中校
  • 技术积分: 6075
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-10 00:11
个人简介

邮箱: wangcong02345@163.com

文章分类

全部博文(438)

文章存档

2017年(15)

2016年(119)

2015年(91)

2014年(62)

2013年(56)

2012年(79)

2011年(16)

分类: Android平台

2014-02-13 15:48:15

g_sensor好像很简单,就是注册i2c设备,然后通过一个任务队列,每隔delay时间向上报x,y,z数据.
在drivers/input/sensors/accel/bma2x2.c中
module_init(BMA2X2_init);
  1. static int __init BMA2X2_init(void)
  2. {
  3.     //注册了一个i2c设备驱动
  4.     return i2c_add_driver(&bma2x2_driver);
  5. }
在probe函数中
  1. static int bma2x2_probe(struct i2c_client *client, const struct i2c_device_id *id)
  2. {
  3.     int err = 0;
  4.     int tempvalue;
  5.     unsigned char tmp_chip_id;
  6.     struct input_dev *dev;

  7.     struct bma2x2_data *data = kzalloc(sizeof(struct bma2x2_data), GFP_KERNEL);
  8.     //获取设备的具体型号
  9.     tempvalue = i2c_smbus_read_word_data(client, BMA2X2_CHIP_ID_REG);
  10.     tmp_chip_id = tempvalue&0x00ff;
  11.     switch (tmp_chip_id) {
  12.     case BMA255_CHIP_ID:
  13.         data->sensor_type = BMA255_TYPE;
  14.         break;
  15.     case BMA250E_CHIP_ID:
  16.         data->sensor_type = BMA250E_TYPE;
  17.         break;
  18.     case BMA222E_CHIP_ID:
  19.         data->sensor_type = BMA222E_TYPE;
  20.         break;
  21.     case BMA280_CHIP_ID:
  22.         data->sensor_type = BMA280_TYPE;
  23.         break;
  24.     default:
  25.         data->sensor_type = -1;
  26.     }

  27.     if (data->sensor_type != -1) {
  28.         data->chip_id = tmp_chip_id;
  29.     } else{
  30.         err = -ENODEV;
  31.         goto kfree_exit;
  32.     }
  33.     i2c_set_clientdata(client, data);
  34.     data->bma2x2_client = client;
  35.     mutex_init(&data->value_mutex);
  36.     mutex_init(&data->mode_mutex);
  37.     mutex_init(&data->enable_mutex);
  38.     bma2x2_set_bandwidth(client, BMA2X2_BW_SET);
  39.     bma2x2_set_range(client, BMA2X2_RANGE_SET);
  40.     bma2x2_set_Int_Enable(client, 8, 1);
  41.     bma2x2_set_Int_Enable(client, 10, 1);
  42.     bma2x2_set_Int_Enable(client, 11, 1);

  43.     INIT_DELAYED_WORK(&data->work, bma2x2_work_func);
  44.     atomic_set(&data->delay, BMA2X2_MAX_DELAY);       //将delay设为8ms
  45.     atomic_set(&data->enable, 0);

  46.     dev = input_allocate_device();
  47.     dev->name = SENSOR_NAME;
  48.     dev->id.bustype = BUS_I2C;

  49.     input_set_capability(dev, EV_REL, LOW_G_INTERRUPT);
  50.     input_set_capability(dev, EV_REL, HIGH_G_INTERRUPT);
  51.     input_set_capability(dev, EV_REL, SLOP_INTERRUPT);
  52.     input_set_capability(dev, EV_REL, DOUBLE_TAP_INTERRUPT);
  53.     input_set_capability(dev, EV_REL, SINGLE_TAP_INTERRUPT);
  54.     input_set_capability(dev, EV_ABS, ORIENT_INTERRUPT);
  55.     input_set_capability(dev, EV_ABS, FLAT_INTERRUPT);
  56.     input_set_abs_params(dev, ABS_X, ABSMIN, ABSMAX, 0, 0);
  57.     input_set_abs_params(dev, ABS_Y, ABSMIN, ABSMAX, 0, 0);
  58.     input_set_abs_params(dev, ABS_Z, ABSMIN, ABSMAX, 0, 0);

  59.     input_set_drvdata(dev, data);

  60.     input_register_device(dev);

  61.     data->input = dev;

  62.     schedule_work(&data->work);

  63.     return 0;
  64. }




  1. static int bma2x2_read_accel_xyz(struct i2c_client *client, signed char sensor_type, struct bma2x2acc *acc)
  2. {
  3.     int comres = 0;
  4.     unsigned char data[6];
  5.     comres = bma2x2_smbus_read_byte_block(client, BMA2X2_ACC_X12_LSB__REG, data, 6);
  6.     acc->x = (data[1]<<8)|data[0];
  7.     acc->y = (data[3]<<8)|data[2];
  8.     acc->z = (data[5]<<8)|data[4];

  9.     return comres;
  10. }

  11. static void bma2x2_work_func(struct work_struct *work)
  12. {
  13.     struct bma2x2_data *bma2x2 = container_of((struct delayed_work *)work, struct bma2x2_data, work);
  14.     static struct bma2x2acc acc;
  15.     unsigned long delay = msecs_to_jiffies(atomic_read(&bma2x2->delay));
  16.     bma2x2_read_accel_xyz(bma2x2->bma2x2_client, bma2x2->sensor_type, &acc);
  17.     input_report_abs(bma2x2->input, ABS_X, -acc.y);
  18.     input_report_abs(bma2x2->input, ABS_Y, acc.x);
  19.     input_report_abs(bma2x2->input, ABS_Z, acc.z);
  20.     input_sync(bma2x2->input);
  21.     schedule_delayed_work(&bma2x2->work, delay);     //每隔8ms上报一次
  22. }


阅读(3682) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~