mma8451.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2013, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*!
  31. * @file
  32. * @brief Public interface for the MMA8451 accelerometer driver.
  33. * @ingroup diag_accel
  34. */
  35. #ifndef __MMA8451_H__
  36. #define __MMA8451_H__
  37. #include "sdk_types.h"
  38. #include "i2c/imx_i2c.h"
  39. //! @addtogroup diag_accel
  40. //! @{
  41. ////////////////////////////////////////////////////////////////////////////////
  42. // Definitions
  43. ////////////////////////////////////////////////////////////////////////////////
  44. //! @brief Error codes for the MMA8451 driver.
  45. enum _mma8451_errors
  46. {
  47. kMMA8451_Invalid_I2C_Address_Error = -128
  48. };
  49. //! @brief Register definitions for the MMA8451.
  50. enum _mma8451_constants
  51. {
  52. kMMA8451_STATUS = 0x00,
  53. kMMA8451_OUT_X_MSB = 0x01,
  54. kMMA8451_OUT_X_LSB = 0x02,
  55. kMMA8451_OUT_Y_MSB = 0x03,
  56. kMMA8451_OUT_Y_LSB = 0x04,
  57. kMMA8451_OUT_Z_MSB = 0x05,
  58. kMMA8451_OUT_Z_LSB = 0x06,
  59. kMMA8451_F_SETUP = 0x09,
  60. kMMA8451_TRIG_CFG = 0x0a,
  61. kMMA8451_SYSMOD = 0x0b,
  62. kMMA8451_INT_SOURCE = 0x0c,
  63. kMMA8451_WHO_AM_I = 0x0d,
  64. kMMA8451_WHO_AM_I_Device_ID = 0x1a,
  65. kMMA8451_XYZ_DATA_CFG = 0x0e,
  66. kMMA8451_CTRL_REG1 = 0x2a,
  67. kMMA8451_CTRL_REG2 = 0x2b,
  68. kMMA8451_CTRL_REG3 = 0x2c,
  69. kMMA8451_CTRL_REG4 = 0x2d,
  70. kMMA8451_CTRL_REG5 = 0x2e
  71. };
  72. //! @brief
  73. typedef struct _mma8451_info {
  74. i2c_device_info_t addressInfo;
  75. } mma8451_device_t;
  76. //! @brief 3D acceleration values.
  77. typedef struct _acceleration {
  78. float x;
  79. float y;
  80. float z;
  81. } acceleration_t;
  82. ////////////////////////////////////////////////////////////////////////////////
  83. // API
  84. ////////////////////////////////////////////////////////////////////////////////
  85. #if defined(__cplusplus)
  86. extern "C" {
  87. #endif
  88. /*!
  89. * @brief Initialize the MMA8451 driver instance.
  90. *
  91. * @param device Device driver state structure that will be filled in by this function.
  92. * It is the responsibility of the caller to provide storage for this structure, and
  93. * to free that storage when the driver is no longer needed.
  94. * @param address Pointer to the I2C address structure for the MMA8451. The device
  95. * address field is checked against the possible addresses for the MMA8451, and
  96. * if it is invalid then kMMA8451_Invalid_I2C_Address_Error will be returned.
  97. * @return SUCCESS or error code.
  98. * @retval SUCCESS
  99. * @retval kMMA8451_Invalid_I2C_Address_Error
  100. */
  101. int mma8451_init(mma8451_device_t * device, const i2c_device_info_t * address);
  102. /*!
  103. * @brief Read the current acceleration values.
  104. *
  105. * @param device Pointer to a valid device instance structure.
  106. * @param accel Pointer to a structure that will be filled in with the current acceleration
  107. * values for all three axes.
  108. * @return SUCCESS or error code.
  109. * @retval SUCCESS
  110. */
  111. int mma8451_get_acceleration(const mma8451_device_t * device, acceleration_t * accel);
  112. /*!
  113. * @brief Read a single register of the MMA8451.
  114. *
  115. * @param device Pointer to a valid device instance structure.
  116. * @param reg_addr Address of the internal MMA8451 register to read.
  117. * @return The register's value.
  118. */
  119. uint8_t mma8451_read_register(const mma8451_device_t * device, uint8_t reg_addr);
  120. /*!
  121. * @brief Write a single register of the MMA8451.
  122. *
  123. * @param device Pointer to a valid device instance structure.
  124. * @param reg_addrAddress of the internal MMA8451 register to change the value of.
  125. * @param reg_val New value for the register.
  126. * @return SUCCESS or error code.
  127. * @retval SUCCESS
  128. */
  129. int mma8451_write_register(const mma8451_device_t * device, uint8_t reg_addr, uint8_t reg_val);
  130. #if defined(__cplusplus)
  131. }
  132. #endif
  133. //! @}
  134. #endif //__MMA8451_H__
  135. ////////////////////////////////////////////////////////////////////////////////
  136. // EOF
  137. ////////////////////////////////////////////////////////////////////////////////