lis3.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * This is device realize "read through write" paradigm. This is not
  15. * standard, but most of I2C devices use this paradigm.
  16. * You must write to device reading address, send restart to bus,
  17. * and then begin reading process.
  18. */
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include <string.h>
  22. #include "ch.h"
  23. #include "hal.h"
  24. #include "lis3.h"
  25. /* device I2C address */
  26. #define addr 0b0011101
  27. /* enable single byte read checks. Note: it does not work on STM32F1x */
  28. #define TEST_SINGLE_BYTE_READ TRUE
  29. /* autoincrement bit position. This bit needs to perform reading of
  30. * multiple bytes at one request */
  31. #define AUTO_INCREMENT_BIT (1<<7)
  32. /* slave specific addresses */
  33. #define ACCEL_STATUS_REG 0x27
  34. #define ACCEL_CTRL_REG1 0x20
  35. #define ACCEL_OUT_DATA 0x28
  36. /* buffers */
  37. static uint8_t accel_rx_data[8];
  38. static uint8_t accel_tx_data[8];
  39. /*
  40. *
  41. */
  42. void lis3Start(void){
  43. msg_t status = MSG_OK;
  44. sysinterval_t tmo = TIME_MS2I(4);
  45. /* configure accelerometer */
  46. accel_tx_data[0] = ACCEL_CTRL_REG1 | AUTO_INCREMENT_BIT;
  47. accel_tx_data[1] = 0b11100111;
  48. accel_tx_data[2] = 0b01000001;
  49. accel_tx_data[3] = 0b00000000;
  50. /* sending */
  51. i2cAcquireBus(&I2CD1);
  52. status = i2cMasterTransmitTimeout(&I2CD1, addr,
  53. accel_tx_data, 4, NULL, 0, tmo);
  54. i2cReleaseBus(&I2CD1);
  55. osalDbgCheck(MSG_OK == status);
  56. }
  57. /*
  58. *
  59. */
  60. static void raw2g(uint8_t *raw, float *g) {
  61. int16_t tmp;
  62. for (size_t i=0; i<3; i++){
  63. tmp = raw[i*2] | (raw[i*2+1] << 8);
  64. g[i] = (float)tmp / 16384.0; /* convert raw value to G */
  65. }
  66. }
  67. /*
  68. *
  69. */
  70. void lis3GetAcc(float *result) {
  71. msg_t status = MSG_OK;
  72. sysinterval_t tmo = TIME_MS2I(4);
  73. /* read in burst mode */
  74. memset(accel_rx_data, 0x55, sizeof(accel_rx_data));
  75. accel_tx_data[0] = ACCEL_OUT_DATA | AUTO_INCREMENT_BIT;
  76. i2cAcquireBus(&I2CD1);
  77. status = i2cMasterTransmitTimeout(&I2CD1, addr,
  78. accel_tx_data, 1, accel_rx_data, 6, tmo);
  79. i2cReleaseBus(&I2CD1);
  80. osalDbgCheck(MSG_OK == status);
  81. raw2g(accel_rx_data, result);
  82. #if TEST_SINGLE_BYTE_READ
  83. float accel_single_byte_check[3];
  84. const float check_threshold = 0.1;
  85. /* read data byte at a time */
  86. memset(accel_rx_data, 0x55, sizeof(accel_rx_data));
  87. accel_tx_data[0] = ACCEL_OUT_DATA;
  88. i2cAcquireBus(&I2CD1);
  89. for (size_t i=0; i<6; i++) {
  90. status = i2cMasterTransmitTimeout(&I2CD1, addr,
  91. accel_tx_data, 1, &accel_rx_data[i], 1, tmo);
  92. osalDbgCheck(MSG_OK == status);
  93. accel_tx_data[0]++;
  94. }
  95. i2cReleaseBus(&I2CD1);
  96. raw2g(accel_rx_data, accel_single_byte_check);
  97. /* check results */
  98. for (size_t i=0; i<3; i++) {
  99. osalDbgCheck(fabsf(result[i] - accel_single_byte_check[i]) < check_threshold);
  100. }
  101. #endif /* TEST_SINGLE_BYTE_READ */
  102. }