搜索
bottom↓
回复: 4

求一份AK8975 基于stm32f1系列的驱动

[复制链接]

出0入0汤圆

发表于 2015-3-22 10:59:47 | 显示全部楼层 |阅读模式
最近想试试软磁条的巡线,找了一款可以修改iic地址的磁场传感器,,网上搜不到stm32的代码,求万能的论坛给一个。。不想自己去写驱动了。代码到时候共享出来免费下载

出0入0汤圆

发表于 2015-3-22 10:59:48 | 显示全部楼层
  1. /*
  2. * A sensor driver for the magnetometer AK8975.
  3. *
  4. * Magnetic compass sensor driver for monitoring magnetic flux information.
  5. *
  6. * Copyright (c) 2010, NVIDIA Corporation.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA        02110-1301, USA.
  21. */

  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/i2c.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/err.h>
  28. #include <linux/mutex.h>
  29. #include <linux/delay.h>
  30. #include <linux/bitops.h>
  31. #include <linux/gpio.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/acpi.h>

  34. #include <linux/iio/iio.h>
  35. #include <linux/iio/sysfs.h>
  36. /*
  37. * Register definitions, as well as various shifts and masks to get at the
  38. * individual fields of the registers.
  39. */
  40. #define AK8975_REG_WIA                        0x00
  41. #define AK8975_DEVICE_ID                0x48

  42. #define AK8975_REG_INFO                        0x01

  43. #define AK8975_REG_ST1                        0x02
  44. #define AK8975_REG_ST1_DRDY_SHIFT        0
  45. #define AK8975_REG_ST1_DRDY_MASK        (1 << AK8975_REG_ST1_DRDY_SHIFT)

  46. #define AK8975_REG_HXL                        0x03
  47. #define AK8975_REG_HXH                        0x04
  48. #define AK8975_REG_HYL                        0x05
  49. #define AK8975_REG_HYH                        0x06
  50. #define AK8975_REG_HZL                        0x07
  51. #define AK8975_REG_HZH                        0x08
  52. #define AK8975_REG_ST2                        0x09
  53. #define AK8975_REG_ST2_DERR_SHIFT        2
  54. #define AK8975_REG_ST2_DERR_MASK        (1 << AK8975_REG_ST2_DERR_SHIFT)

  55. #define AK8975_REG_ST2_HOFL_SHIFT        3
  56. #define AK8975_REG_ST2_HOFL_MASK        (1 << AK8975_REG_ST2_HOFL_SHIFT)

  57. #define AK8975_REG_CNTL                        0x0A
  58. #define AK8975_REG_CNTL_MODE_SHIFT        0
  59. #define AK8975_REG_CNTL_MODE_MASK        (0xF << AK8975_REG_CNTL_MODE_SHIFT)
  60. #define AK8975_REG_CNTL_MODE_POWER_DOWN        0
  61. #define AK8975_REG_CNTL_MODE_ONCE        1
  62. #define AK8975_REG_CNTL_MODE_SELF_TEST        8
  63. #define AK8975_REG_CNTL_MODE_FUSE_ROM        0xF

  64. #define AK8975_REG_RSVC                        0x0B
  65. #define AK8975_REG_ASTC                        0x0C
  66. #define AK8975_REG_TS1                        0x0D
  67. #define AK8975_REG_TS2                        0x0E
  68. #define AK8975_REG_I2CDIS                0x0F
  69. #define AK8975_REG_ASAX                        0x10
  70. #define AK8975_REG_ASAY                        0x11
  71. #define AK8975_REG_ASAZ                        0x12

  72. #define AK8975_MAX_REGS                        AK8975_REG_ASAZ

  73. /*
  74. * Miscellaneous values.
  75. */
  76. #define AK8975_MAX_CONVERSION_TIMEOUT        500
  77. #define AK8975_CONVERSION_DONE_POLL_TIME 10
  78. #define AK8975_DATA_READY_TIMEOUT        ((100*HZ)/1000)
  79. #define RAW_TO_GAUSS_8975(asa) ((((asa) + 128) * 3000) / 256)
  80. #define RAW_TO_GAUSS_8963(asa) ((((asa) + 128) * 6000) / 256)

  81. /* Compatible Asahi Kasei Compass parts */
  82. enum asahi_compass_chipset {
  83.         AK8975,
  84.         AK8963,
  85. };

  86. /*
  87. * Per-instance context data for the device.
  88. */
  89. struct ak8975_data {
  90.         struct i2c_client        *client;
  91.         struct attribute_group        attrs;
  92.         struct mutex                lock;
  93.         u8                        asa[3];
  94.         long                        raw_to_gauss[3];
  95.         u8                        reg_cache[AK8975_MAX_REGS];
  96.         int                        eoc_gpio;
  97.         int                        eoc_irq;
  98.         wait_queue_head_t        data_ready_queue;
  99.         unsigned long                flags;
  100.         enum asahi_compass_chipset chipset;
  101. };

  102. static const int ak8975_index_to_reg[] = {
  103.         AK8975_REG_HXL, AK8975_REG_HYL, AK8975_REG_HZL,
  104. };

  105. /*
  106. * Helper function to write to the I2C device's registers.
  107. */
  108. static int ak8975_write_data(struct i2c_client *client,
  109.                              u8 reg, u8 val, u8 mask, u8 shift)
  110. {
  111.         struct iio_dev *indio_dev = i2c_get_clientdata(client);
  112.         struct ak8975_data *data = iio_priv(indio_dev);
  113.         u8 regval;
  114.         int ret;

  115.         regval = (data->reg_cache[reg] & ~mask) | (val << shift);
  116.         ret = i2c_smbus_write_byte_data(client, reg, regval);
  117.         if (ret < 0) {
  118.                 dev_err(&client->dev, "Write to device fails status %x\n", ret);
  119.                 return ret;
  120.         }
  121.         data->reg_cache[reg] = regval;

  122.         return 0;
  123. }

  124. /*
  125. * Handle data ready irq
  126. */
  127. static irqreturn_t ak8975_irq_handler(int irq, void *data)
  128. {
  129.         struct ak8975_data *ak8975 = data;

  130.         set_bit(0, &ak8975->flags);
  131.         wake_up(&ak8975->data_ready_queue);

  132.         return IRQ_HANDLED;
  133. }

  134. /*
  135. * Install data ready interrupt handler
  136. */
  137. static int ak8975_setup_irq(struct ak8975_data *data)
  138. {
  139.         struct i2c_client *client = data->client;
  140.         int rc;
  141.         int irq;

  142.         if (client->irq)
  143.                 irq = client->irq;
  144.         else
  145.                 irq = gpio_to_irq(data->eoc_gpio);

  146.         rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
  147.                          IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  148.                          dev_name(&client->dev), data);
  149.         if (rc < 0) {
  150.                 dev_err(&client->dev,
  151.                         "irq %d request failed, (gpio %d): %d\n",
  152.                         irq, data->eoc_gpio, rc);
  153.                 return rc;
  154.         }

  155.         init_waitqueue_head(&data->data_ready_queue);
  156.         clear_bit(0, &data->flags);
  157.         data->eoc_irq = irq;

  158.         return rc;
  159. }


  160. /*
  161. * Perform some start-of-day setup, including reading the asa calibration
  162. * values and caching them.
  163. */
  164. static int ak8975_setup(struct i2c_client *client)
  165. {
  166.         struct iio_dev *indio_dev = i2c_get_clientdata(client);
  167.         struct ak8975_data *data = iio_priv(indio_dev);
  168.         u8 device_id;
  169.         int ret;

  170.         /* Confirm that the device we're talking to is really an AK8975. */
  171.         ret = i2c_smbus_read_byte_data(client, AK8975_REG_WIA);
  172.         if (ret < 0) {
  173.                 dev_err(&client->dev, "Error reading WIA\n");
  174.                 return ret;
  175.         }
  176.         device_id = ret;
  177.         if (device_id != AK8975_DEVICE_ID) {
  178.                 dev_err(&client->dev, "Device ak8975 not found\n");
  179.                 return -ENODEV;
  180.         }

  181.         /* Write the fused rom access mode. */
  182.         ret = ak8975_write_data(client,
  183.                                 AK8975_REG_CNTL,
  184.                                 AK8975_REG_CNTL_MODE_FUSE_ROM,
  185.                                 AK8975_REG_CNTL_MODE_MASK,
  186.                                 AK8975_REG_CNTL_MODE_SHIFT);
  187.         if (ret < 0) {
  188.                 dev_err(&client->dev, "Error in setting fuse access mode\n");
  189.                 return ret;
  190.         }

  191.         /* Get asa data and store in the device data. */
  192.         ret = i2c_smbus_read_i2c_block_data(client, AK8975_REG_ASAX,
  193.                                             3, data->asa);
  194.         if (ret < 0) {
  195.                 dev_err(&client->dev, "Not able to read asa data\n");
  196.                 return ret;
  197.         }

  198.         /* After reading fuse ROM data set power-down mode */
  199.         ret = ak8975_write_data(client,
  200.                                 AK8975_REG_CNTL,
  201.                                 AK8975_REG_CNTL_MODE_POWER_DOWN,
  202.                                 AK8975_REG_CNTL_MODE_MASK,
  203.                                 AK8975_REG_CNTL_MODE_SHIFT);

  204.         if (data->eoc_gpio > 0 || client->irq) {
  205.                 ret = ak8975_setup_irq(data);
  206.                 if (ret < 0) {
  207.                         dev_err(&client->dev,
  208.                                 "Error setting data ready interrupt\n");
  209.                         return ret;
  210.                 }
  211.         }

  212.         if (ret < 0) {
  213.                 dev_err(&client->dev, "Error in setting power-down mode\n");
  214.                 return ret;
  215.         }

  216. /*
  217. * Precalculate scale factor (in Gauss units) for each axis and
  218. * store in the device data.
  219. *
  220. * This scale factor is axis-dependent, and is derived from 3 calibration
  221. * factors ASA(x), ASA(y), and ASA(z).
  222. *
  223. * These ASA values are read from the sensor device at start of day, and
  224. * cached in the device context struct.
  225. *
  226. * Adjusting the flux value with the sensitivity adjustment value should be
  227. * done via the following formula:
  228. *
  229. * Hadj = H * ( ( ( (ASA-128)*0.5 ) / 128 ) + 1 )
  230. *
  231. * where H is the raw value, ASA is the sensitivity adjustment, and Hadj
  232. * is the resultant adjusted value.
  233. *
  234. * We reduce the formula to:
  235. *
  236. * Hadj = H * (ASA + 128) / 256
  237. *
  238. * H is in the range of -4096 to 4095.  The magnetometer has a range of
  239. * +-1229uT.  To go from the raw value to uT is:
  240. *
  241. * HuT = H * 1229/4096, or roughly, 3/10.
  242. *
  243. * Since 1uT = 0.01 gauss, our final scale factor becomes:
  244. *
  245. * Hadj = H * ((ASA + 128) / 256) * 3/10 * 1/100
  246. * Hadj = H * ((ASA + 128) * 0.003) / 256
  247. *
  248. * Since ASA doesn't change, we cache the resultant scale factor into the
  249. * device context in ak8975_setup().
  250. */
  251.         if (data->chipset == AK8963) {
  252.                 /*
  253.                  * H range is +-8190 and magnetometer range is +-4912.
  254.                  * So HuT using the above explanation for 8975,
  255.                  * 4912/8190 = ~ 6/10.
  256.                  * So the Hadj should use 6/10 instead of 3/10.
  257.                  */
  258.                 data->raw_to_gauss[0] = RAW_TO_GAUSS_8963(data->asa[0]);
  259.                 data->raw_to_gauss[1] = RAW_TO_GAUSS_8963(data->asa[1]);
  260.                 data->raw_to_gauss[2] = RAW_TO_GAUSS_8963(data->asa[2]);
  261.         } else {
  262.                 data->raw_to_gauss[0] = RAW_TO_GAUSS_8975(data->asa[0]);
  263.                 data->raw_to_gauss[1] = RAW_TO_GAUSS_8975(data->asa[1]);
  264.                 data->raw_to_gauss[2] = RAW_TO_GAUSS_8975(data->asa[2]);
  265.         }

  266.         return 0;
  267. }

  268. static int wait_conversion_complete_gpio(struct ak8975_data *data)
  269. {
  270.         struct i2c_client *client = data->client;
  271.         u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
  272.         int ret;

  273.         /* Wait for the conversion to complete. */
  274.         while (timeout_ms) {
  275.                 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
  276.                 if (gpio_get_value(data->eoc_gpio))
  277.                         break;
  278.                 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
  279.         }
  280.         if (!timeout_ms) {
  281.                 dev_err(&client->dev, "Conversion timeout happened\n");
  282.                 return -EINVAL;
  283.         }

  284.         ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
  285.         if (ret < 0)
  286.                 dev_err(&client->dev, "Error in reading ST1\n");

  287.         return ret;
  288. }

  289. static int wait_conversion_complete_polled(struct ak8975_data *data)
  290. {
  291.         struct i2c_client *client = data->client;
  292.         u8 read_status;
  293.         u32 timeout_ms = AK8975_MAX_CONVERSION_TIMEOUT;
  294.         int ret;

  295.         /* Wait for the conversion to complete. */
  296.         while (timeout_ms) {
  297.                 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
  298.                 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST1);
  299.                 if (ret < 0) {
  300.                         dev_err(&client->dev, "Error in reading ST1\n");
  301.                         return ret;
  302.                 }
  303.                 read_status = ret;
  304.                 if (read_status)
  305.                         break;
  306.                 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
  307.         }
  308.         if (!timeout_ms) {
  309.                 dev_err(&client->dev, "Conversion timeout happened\n");
  310.                 return -EINVAL;
  311.         }

  312.         return read_status;
  313. }

  314. /* Returns 0 if the end of conversion interrupt occured or -ETIME otherwise */
  315. static int wait_conversion_complete_interrupt(struct ak8975_data *data)
  316. {
  317.         int ret;

  318.         ret = wait_event_timeout(data->data_ready_queue,
  319.                                  test_bit(0, &data->flags),
  320.                                  AK8975_DATA_READY_TIMEOUT);
  321.         clear_bit(0, &data->flags);

  322.         return ret > 0 ? 0 : -ETIME;
  323. }

  324. /*
  325. * Emits the raw flux value for the x, y, or z axis.
  326. */
  327. static int ak8975_read_axis(struct iio_dev *indio_dev, int index, int *val)
  328. {
  329.         struct ak8975_data *data = iio_priv(indio_dev);
  330.         struct i2c_client *client = data->client;
  331.         int ret;

  332.         mutex_lock(&data->lock);

  333.         /* Set up the device for taking a sample. */
  334.         ret = ak8975_write_data(client,
  335.                                 AK8975_REG_CNTL,
  336.                                 AK8975_REG_CNTL_MODE_ONCE,
  337.                                 AK8975_REG_CNTL_MODE_MASK,
  338.                                 AK8975_REG_CNTL_MODE_SHIFT);
  339.         if (ret < 0) {
  340.                 dev_err(&client->dev, "Error in setting operating mode\n");
  341.                 goto exit;
  342.         }

  343.         /* Wait for the conversion to complete. */
  344.         if (data->eoc_irq)
  345.                 ret = wait_conversion_complete_interrupt(data);
  346.         else if (gpio_is_valid(data->eoc_gpio))
  347.                 ret = wait_conversion_complete_gpio(data);
  348.         else
  349.                 ret = wait_conversion_complete_polled(data);
  350.         if (ret < 0)
  351.                 goto exit;

  352.         /* This will be executed only for non-interrupt based waiting case */
  353.         if (ret & AK8975_REG_ST1_DRDY_MASK) {
  354.                 ret = i2c_smbus_read_byte_data(client, AK8975_REG_ST2);
  355.                 if (ret < 0) {
  356.                         dev_err(&client->dev, "Error in reading ST2\n");
  357.                         goto exit;
  358.                 }
  359.                 if (ret & (AK8975_REG_ST2_DERR_MASK |
  360.                            AK8975_REG_ST2_HOFL_MASK)) {
  361.                         dev_err(&client->dev, "ST2 status error 0x%x\n", ret);
  362.                         ret = -EINVAL;
  363.                         goto exit;
  364.                 }
  365.         }

  366.         /* Read the flux value from the appropriate register
  367.            (the register is specified in the iio device attributes). */
  368.         ret = i2c_smbus_read_word_data(client, ak8975_index_to_reg[index]);
  369.         if (ret < 0) {
  370.                 dev_err(&client->dev, "Read axis data fails\n");
  371.                 goto exit;
  372.         }

  373.         mutex_unlock(&data->lock);

  374.         /* Clamp to valid range. */
  375.         *val = clamp_t(s16, ret, -4096, 4095);
  376.         return IIO_VAL_INT;

  377. exit:
  378.         mutex_unlock(&data->lock);
  379.         return ret;
  380. }

  381. static int ak8975_read_raw(struct iio_dev *indio_dev,
  382.                            struct iio_chan_spec const *chan,
  383.                            int *val, int *val2,
  384.                            long mask)
  385. {
  386.         struct ak8975_data *data = iio_priv(indio_dev);

  387.         switch (mask) {
  388.         case IIO_CHAN_INFO_RAW:
  389.                 return ak8975_read_axis(indio_dev, chan->address, val);
  390.         case IIO_CHAN_INFO_SCALE:
  391.                 *val = 0;
  392.                 *val2 = data->raw_to_gauss[chan->address];
  393.                 return IIO_VAL_INT_PLUS_MICRO;
  394.         }
  395.         return -EINVAL;
  396. }

  397. #define AK8975_CHANNEL(axis, index)                                        \
  398.         {                                                                \
  399.                 .type = IIO_MAGN,                                        \
  400.                 .modified = 1,                                                \
  401.                 .channel2 = IIO_MOD_##axis,                                \
  402.                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |                \
  403.                              BIT(IIO_CHAN_INFO_SCALE),                        \
  404.                 .address = index,                                        \
  405.         }

  406. static const struct iio_chan_spec ak8975_channels[] = {
  407.         AK8975_CHANNEL(X, 0), AK8975_CHANNEL(Y, 1), AK8975_CHANNEL(Z, 2),
  408. };

  409. static const struct iio_info ak8975_info = {
  410.         .read_raw = &ak8975_read_raw,
  411.         .driver_module = THIS_MODULE,
  412. };

  413. static const struct acpi_device_id ak_acpi_match[] = {
  414.         {"AK8975", AK8975},
  415.         {"AK8963", AK8963},
  416.         {"INVN6500", AK8963},
  417.         { },
  418. };
  419. MODULE_DEVICE_TABLE(acpi, ak_acpi_match);

  420. static const char *ak8975_match_acpi_device(struct device *dev,
  421.                                             enum asahi_compass_chipset *chipset)
  422. {
  423.         const struct acpi_device_id *id;

  424.         id = acpi_match_device(dev->driver->acpi_match_table, dev);
  425.         if (!id)
  426.                 return NULL;
  427.         *chipset = (int)id->driver_data;

  428.         return dev_name(dev);
  429. }

  430. static int ak8975_probe(struct i2c_client *client,
  431.                         const struct i2c_device_id *id)
  432. {
  433.         struct ak8975_data *data;
  434.         struct iio_dev *indio_dev;
  435.         int eoc_gpio;
  436.         int err;
  437.         const char *name = NULL;

  438.         /* Grab and set up the supplied GPIO. */
  439.         if (client->dev.platform_data)
  440.                 eoc_gpio = *(int *)(client->dev.platform_data);
  441.         else if (client->dev.of_node)
  442.                 eoc_gpio = of_get_gpio(client->dev.of_node, 0);
  443.         else
  444.                 eoc_gpio = -1;

  445.         if (eoc_gpio == -EPROBE_DEFER)
  446.                 return -EPROBE_DEFER;

  447.         /* We may not have a GPIO based IRQ to scan, that is fine, we will
  448.            poll if so */
  449.         if (gpio_is_valid(eoc_gpio)) {
  450.                 err = devm_gpio_request_one(&client->dev, eoc_gpio,
  451.                                                         GPIOF_IN, "ak_8975");
  452.                 if (err < 0) {
  453.                         dev_err(&client->dev,
  454.                                 "failed to request GPIO %d, error %d\n",
  455.                                                         eoc_gpio, err);
  456.                         return err;
  457.                 }
  458.         }

  459.         /* Register with IIO */
  460.         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  461.         if (indio_dev == NULL)
  462.                 return -ENOMEM;

  463.         data = iio_priv(indio_dev);
  464.         i2c_set_clientdata(client, indio_dev);

  465.         data->client = client;
  466.         data->eoc_gpio = eoc_gpio;
  467.         data->eoc_irq = 0;

  468.         /* id will be NULL when enumerated via ACPI */
  469.         if (id) {
  470.                 data->chipset =
  471.                         (enum asahi_compass_chipset)(id->driver_data);
  472.                 name = id->name;
  473.         } else if (ACPI_HANDLE(&client->dev))
  474.                 name = ak8975_match_acpi_device(&client->dev, &data->chipset);
  475.         else
  476.                 return -ENOSYS;

  477.         dev_dbg(&client->dev, "Asahi compass chip %s\n", name);

  478.         /* Perform some basic start-of-day setup of the device. */
  479.         err = ak8975_setup(client);
  480.         if (err < 0) {
  481.                 dev_err(&client->dev, "AK8975 initialization fails\n");
  482.                 return err;
  483.         }

  484.         data->client = client;
  485.         mutex_init(&data->lock);
  486.         data->eoc_gpio = eoc_gpio;
  487.         indio_dev->dev.parent = &client->dev;
  488.         indio_dev->channels = ak8975_channels;
  489.         indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
  490.         indio_dev->info = &ak8975_info;
  491.         indio_dev->modes = INDIO_DIRECT_MODE;
  492.         indio_dev->name = name;
  493.         err = devm_iio_device_register(&client->dev, indio_dev);
  494.         if (err < 0)
  495.                 return err;

  496.         return 0;
  497. }

  498. static const struct i2c_device_id ak8975_id[] = {
  499.         {"ak8975", AK8975},
  500.         {"ak8963", AK8963},
  501.         {}
  502. };

  503. MODULE_DEVICE_TABLE(i2c, ak8975_id);

  504. static const struct of_device_id ak8975_of_match[] = {
  505.         { .compatible = "asahi-kasei,ak8975", },
  506.         { .compatible = "ak8975", },
  507.         { }
  508. };
  509. MODULE_DEVICE_TABLE(of, ak8975_of_match);

  510. static struct i2c_driver ak8975_driver = {
  511.         .driver = {
  512.                 .name        = "ak8975",
  513.                 .of_match_table = ak8975_of_match,
  514.                 .acpi_match_table = ACPI_PTR(ak_acpi_match),
  515.         },
  516.         .probe                = ak8975_probe,
  517.         .id_table        = ak8975_id,
  518. };
  519. module_i2c_driver(ak8975_driver);

  520. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  521. MODULE_DESCRIPTION("AK8975 magnetometer driver");
  522. MODULE_LICENSE("GPL");
复制代码

出0入0汤圆

 楼主| 发表于 2015-3-22 19:19:50 | 显示全部楼层
最后自己搞了一下,可以读了贴出来

unsigned char aa=0, bb, data[6];
int x,y,z;
float xx,yy,zz;
int jj;

int f( int x)
{
                if ( x > 0x0fff )
                        return (x-0xf000-4096);
                else
                        return x;
}

void Key_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

          /*开启按键端口(PB0)的时钟*/
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

        GPIO_Init(GPIOB, &GPIO_InitStructure);
}

void scan()
{
        while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0) == 0)
                __nop();//低电平等待
}

int main(void)
{
       
        Key_GPIO_Config();
        I2c_Init();

        aa = Single_Read(0x18,0x00);
       
        while(1)
        {
                //scan();
                bb = Single_Write(0x18,0x0a,1);
                scan();
                data[0] = Single_Read(0x18,0x03);
                data[1] = Single_Read(0x18,0x04);
                data[2] = Single_Read(0x18,0x05);
                data[3] = Single_Read(0x18,0x06);
                data[4] = Single_Read(0x18,0x07);
                data[5] = Single_Read(0x18,0x08);
               
                x = (int)data[1] << 8 | (int)data[0];
                y = (int)data[3] << 8 | (int)data[2];
                z = (int)data[5] << 8 | (int)data[4];
               
                x = f(x);
                y = f(y);
                z = f(z);
               
                xx = (float)x * 0.3;
                yy = (float)y * 0.3;
                zz = (float)z * 0.3;
                                       
        //        for(jj = 0; jj<65530; jj++);               
               
        }
          // add your code here ^_^。
}

出0入0汤圆

 楼主| 发表于 2015-3-22 19:20:34 | 显示全部楼层

感谢分享linux驱动

出0入0汤圆

 楼主| 发表于 2015-3-31 10:31:32 | 显示全部楼层
本帖最后由 熵之矢 于 2015-3-31 10:56 编辑

这是我最后修改的驱动,分享之。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-20 11:10

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表