mma745x.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #ifndef _DEV_MMA745X_H_
  2. #define _DEV_MMA745X_H_
  3. /*
  4. * Copyright (C) 2010 by Rittal GmbH & Co. KG,
  5. * Dawid Sadji <sadji.d@rittal.de> All rights reserved.
  6. * Ulrich Prinz <prinz.u@rittal.de> All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
  25. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. *
  35. */
  36. /*
  37. * \file dev/mma745x.h
  38. * \brief Driver for Freescale MMA745x velocity sensor.
  39. *
  40. * \verbatim
  41. * $Id$
  42. * \endverbatim
  43. */
  44. #ifndef I2C_SLA_MMA745x
  45. #define I2C_SLA_MMA745x 0x1D
  46. #endif
  47. /*
  48. * MMA745x Register Map.
  49. */
  50. #define MMA745x_REG_XOUTL 0x00 /**< ro: Value X-Axis 10 bit resolution LSB */
  51. #define MMA745x_REG_XOUTH 0x01 /**< ro: Value X-Axis 10 bit resolution MSB */
  52. #define MMA745x_REG_YOUTL 0x02 /**< ro: Value Y-Axis 10 bit resolution LSB */
  53. #define MMA745x_REG_YOUTH 0x03 /**< ro: Value Y-Axis 10 bit resolution MSB */
  54. #define MMA745x_REG_ZOUTL 0x04 /**< ro: Value Z-Axis 10 bit resolution LSB */
  55. #define MMA745x_REG_ZOUTH 0x05 /**< ro: Value Z-Axis 10 bit resolution MSB */
  56. #define MMA745x_REG_XOUT8 0x06 /**< ro: Value X-Axis 8 bit resolution */
  57. #define MMA745x_REG_YOUT8 0x07 /**< ro: Value Y-Axis 8 bit resolution */
  58. #define MMA745x_REG_ZOUT8 0x08 /**< ro: Value Z-Axis 8 bit resolution */
  59. #define MMA745x_REG_STATUS 0x09 /**< ro: Status register */
  60. #define MMA745x_REG_DETSRC 0x0A /**< ro: Detection source register */
  61. #define MMA745x_REG_TOUT 0x0B /**< ro: Optional temperature output register */
  62. #define MMA745x_REG_I2CAD 0x0D /**< rw: I2C device address register */
  63. #define MMA745x_REG_USRINF 0x0E /**< ro: Optional user information register */
  64. #define MMA745x_REG_WHOAMI 0x0E /**< ro: Optional chip ID register */
  65. #define MMA745x_REG_XOFFL 0x10 /**< X-Axis offset drift register LSB */
  66. #define MMA745x_REG_XOFFH 0x11 /**< X-Axis offset drift register MSB */
  67. #define MMA745x_REG_YOFFL 0x12 /**< Y-Axis offset drift register LSB */
  68. #define MMA745x_REG_YOFFH 0x13 /**< Y-Axis offset drift register MSB */
  69. #define MMA745x_REG_ZOFFL 0x14 /**< Z-Axis offset drift register LSB */
  70. #define MMA745x_REG_ZOFFH 0x15 /**< Z-Axis offset drift register MSB */
  71. #define MMA745x_REG_MCTL 0x16 /**< Mode control register */
  72. #define MMA745x_REG_INTRST 0x17 /**< Interrupt latch reset register */
  73. #define MMA745x_REG_CTL1 0x18 /**< Control register 1 */
  74. #define MMA745x_REG_CTL2 0x19 /**< Control register 2 */
  75. #define MMA745x_REG_LDTH 0x1A /**< Level detection threshold value */
  76. #define MMA745x_REG_PDTH 0x1B /**< Pulse detection threshold value */
  77. #define MMA745x_REG_PW 0x1C /**< Pulse duration value */
  78. #define MMA745x_REG_LT 0x1D /**< Latency time value */
  79. #define MMA745x_REG_TW 0x1E /**< Time window for 2nd pulse value (double-click detection) */
  80. /*
  81. * MMA745x Status Register definition.
  82. * This register is read only.
  83. */
  84. #define MMA745X_STATUS_DRDY (1 << 0) /**< 1: Data ready */
  85. #define MMA745X_STATUS_DOVR (1 << 1) /**< 1: Overrun (previous data was overwritten before it was read.) */
  86. #define MMA745X_STATUS_PERR (1 << 2) /**< 1: Parity error in trim data, self-test is disabled */
  87. /*
  88. * MMA745x Detection Source Register definition.
  89. * This register is read only.
  90. */
  91. #define MMA745x_DETSRC_INT1 (1 << 0) /**< 1: Interruppt signal INT1 assigned by INTGR detected. */
  92. #define MMA745x_DETSRC_INT2 (1 << 1) /**< 1: Interruppt signal INT2 assigned by INTGR detected. */
  93. #define MMA745x_DETSRC_PDZ (1 << 2) /**< 1: Pulse detection on Z-axis */
  94. #define MMA745x_DETSRC_PDY (1 << 3) /**< 1: Pulse detection on Y-axis */
  95. #define MMA745x_DETSRC_PDX (1 << 4) /**< 1: Pulse detection on X-axis */
  96. #define MMA745x_DETSRC_LDZ (1 << 5) /**< 1: Level detection on Z-axis */
  97. #define MMA745x_DETSRC_LDY (1 << 6) /**< 1: Level detection on Y-axis */
  98. #define MMA745x_DETSRC_LDX (1 << 7) /**< 1: Level detection on X-axis */
  99. /*
  100. * MMA745x I2C Address Register definition.
  101. * This register is read only, bit 7 is read/write.
  102. */
  103. #define MMA745x_I2CAD_I2CDIS (1 << 7) /**< rw: 0: I2C and SPI available / 1: I2C disabled */
  104. #define MMA745x_I2CAD_I2CMSK (0x7F) /**< ro: Mask to read chips I2C address */
  105. /*
  106. * MMA745x Mode Register definitions.
  107. * This register is read/write.
  108. */
  109. #define MMA745X_MCTL_STBY (0x00 << 0) /**< rw: Mode standby */
  110. #define MMA745X_MCTL_MEAS (0x01 << 0) /**< rw: Mode measurement, INT1/DRDY pin may serve as data ready signal. */
  111. #define MMA745X_MCTL_LVL (0x02 << 0) /**< rw: Mode level detection, INTx may serve as level interrupt. */
  112. #define MMA745X_MCTL_PLS (0x03 << 0) /**< rw: Mode pulse detection, INTx may serve as pulse interrupt. */
  113. #define MMA745X_MCTL_MSK (0x03 << 0) /**< Mask mode bits */
  114. #define MMA745X_MCTL_GLVL_8G (0x00 << 2) /**< rw: Measurement range is 8g. */
  115. #define MMA745X_MCTL_GLVL_4G (0x02 << 2) /**< rw: Measurement range is 4g. */
  116. #define MMA745X_MCTL_GLVL_2G (0x01 << 2) /**< rw: Measurement range is 2g. */
  117. #define MMA745X_MCTL_GLVL_MSK (0x03 << 2) /**< Mask measurement range. */
  118. #define MMA745X_MCTL_STON (1 << 4) /**< rw: 1: Self-test is anebled. */
  119. #define MMA745X_MCTL_SPI3W (1 << 5) /**< rw: 1: SPI in 3-wire mode, 0: SPI is 4-wire mode. */
  120. #define MMA745X_MCTL_DRPD (1 << 6) /**< rw: 1: Data ready status is output to INT1/DRDY pin. */
  121. /*
  122. * MMA745x Interrupt Reset Register definitions.
  123. * This register is read/write.
  124. */
  125. #define MMA745x_INTRST_CLRINT1 (1 << 0) /**< rw: 1: Clear INT1, 0: Enable INT1 */
  126. #define MMA745x_INTRST_CLRINT2 (1 << 1) /**< rw: 1: Clear INT2, 0: Enable INT2 */
  127. #define MMA745x_INTRST_MSK (MMA745x_INTRST_CLRINT1 | MMA745x_INTRST_CLRINT2)
  128. /*
  129. * MMA745x Control Register 1 definitions.
  130. * This register is read/write.
  131. */
  132. #define MMA745x_CTL1_INTREV (1 << 0) /**< rw: 0: Routing: sig INT1 to pin INT1/DRDY, signal INT2 to pin INT2
  133. 1: Routing: sig INT1 to pin INT2, signal INT2 to pin INT1/DRDY */
  134. #define MMA745x_CTL1_L1P2 (0x00 << 1) /**< rw: INT1 signal is level detection, INT2 signal is pulse detection */
  135. #define MMA745x_CTL1_P1L2 (0x01 << 1) /**< rw: INT1 signal is pulse detection, INT2 signal is level detection */
  136. #define MMA745x_CTL1_P1P2 (0x02 << 1) /**< rw: INT1 signal is single pulse, INT2 signal is double pulse detection. */
  137. #define MMA745x_CTL1_XDA (1 << 3) /**< rw: 0: Enable / 1: Disable X-axis for detection. */
  138. #define MMA745x_CTL1_YDA (1 << 4) /**< rw: 0: Enable / 1: Disable Y-axis for detection. */
  139. #define MMA745x_CTL1_ZDA (1 << 5) /**< rw: 0: Enable / 1: Disable Z-axis for detection. */
  140. #define MMA745x_CTL1_THOPT (1 << 6) /**< rw: 0: Threshold is absolute, 1: Threshold is signed integer. */
  141. #define MMA745x_CTL1_DFBW (1 << 7) /**< rw: 0: Bandwidth filter is 62.5Hz, 1: 125Hz. */
  142. /*
  143. * MMA745x Control Register 2 definitions.
  144. * This register is read/write.
  145. */
  146. #define MMA745x_CTL2_LDPL (1 << 0) /**< rw: 0: Level detection polarity positive, 3-axes OR-ed.
  147. 1: Level detection polarity negative, 3-axes AND-ed. */
  148. #define MMA745x_CTL2_PDPL (1 << 1) /**< rw: 0: Pulse detection polarity positive, 3-axes OR-ed.
  149. 1: Pulse detection polarity negative, 3-axes AND-ed. */
  150. #define MMA745x_CTL2_DRVO (1 << 2) /**< rw: 0: Standard / 1: strong drive strength on SDA/SDO pin. */
  151. /*
  152. * MMA745x Level Detection Threshold Limit Value.
  153. * This register is read/write.
  154. *
  155. * This register contains the threshold value for level detection.
  156. * If THOPT in CTL1 is 0 it is an unsigned 7 bit value and bit 7 should be 0.
  157. * If THOPT in CTL1 is 1 it is an signed 8 bit value.
  158. */
  159. #define MMA745x_LDTH_SMSK 0x7F /**< Mask for value if THOPT in CTL1 is 0. */
  160. /*
  161. * MMA745x Pulse Detection Threshold Limit Value.
  162. * This register is read/write.
  163. *
  164. * This register contains the threshold value for pulse detection.
  165. * This is an unsigned 7 bit value and bit 7 should be 0.
  166. */
  167. #define MMA745x_PDTH_SMSK 0x7F /**< Mask for value if THOPT in CTL1 is 0. */
  168. /*
  169. * MMA745x Pulse Duration Value.
  170. * This register is read/write.
  171. *
  172. * This register contains the pulse duration value.
  173. * This is an unsigned 8 bit value in 0.5ms steps.
  174. */
  175. #define MMA745x_PW_MSK 0xFF /**< Pulse duration value mask. */
  176. /*
  177. * MMA745x Latency Time Value.
  178. * This register is read/write.
  179. *
  180. * This register contains the latency time for pulse detection.
  181. * This is an unsigned 8 bit value in 1ms steps.
  182. */
  183. #define MMA745x_LT_MSK 0xFF /**< Latency time value mask. */
  184. /*
  185. * MMA745x Double Pulse Detection Time Window Value.
  186. * This register is read/write.
  187. *
  188. * This register contains time window for double pulse detection.
  189. * This is an unsigned 8 bit value in 1ms steps.
  190. */
  191. #define MMA745x_TW_MSK 0xFF /**< Time window for 2nd pulse value (double-click detection) */
  192. #include <cfg/mma745x.h>
  193. #ifndef MMA745X_RANGE
  194. #define MMA745X_RANGE MMA745X_MCTL_GLVL_8G
  195. #endif
  196. #ifndef MMA74xx_MODE
  197. #define MMA74xx_MODE (MMA745X_MCTL_MEAS | MMA745X_RANGE)
  198. #endif
  199. /*! brief MMA7455L 10-bit values and offset register struct
  200. */
  201. typedef struct NUT_PACKED_TYPE
  202. {
  203. int16_t x;
  204. int16_t y;
  205. int16_t z;
  206. } mma10bit_t;
  207. /*! brief MMA7455L 10-bit values and offset register struct
  208. */
  209. typedef struct NUT_PACKED_TYPE
  210. {
  211. int8_t x;
  212. int8_t y;
  213. int8_t z;
  214. } mma8bit_t;
  215. /*! brief MMA7455L initialization struct
  216. */
  217. typedef struct NUT_PACKED_TYPE
  218. {
  219. uint8_t rMODE;
  220. uint8_t rINTRST;
  221. uint8_t rCONTROL1;
  222. uint8_t rCONTROL2;
  223. uint8_t rLEVEL;
  224. uint8_t rPVALUE;
  225. uint8_t rPDUR;
  226. uint8_t rLATTV;
  227. uint8_t rTW;
  228. } mmaInit_t;
  229. /*! brief MMA7455L combined status register
  230. */
  231. typedef struct NUT_PACKED_TYPE
  232. {
  233. uint8_t state;
  234. uint8_t detsrc;
  235. } mmaState_t;
  236. /* brief MMA745x control function calls.
  237. */
  238. #define MMA_GET_STATE 0
  239. #define MMA_SET_MODE 1
  240. #define MMA_GET_IRQ 2
  241. #define MMA_SET_IRQ 3
  242. #define MMA_CLR_IRQ 4
  243. /* Low Level Access Functions */
  244. int Mma745xWrite( uint_fast8_t reg, void *val, size_t len);
  245. int Mma745xRead( uint_fast8_t reg, void *val, size_t len);
  246. /* Raw Value Access Functions */
  247. int Mma745xReadVal8( mma8bit_t *val);
  248. int Mma745xReadVal10( uint8_t ofs, mma10bit_t *val);
  249. /* g Value Access Functions */
  250. int Mma745xReadG( mma10bit_t *val);
  251. /* Calibration Value Access Functions */
  252. int Mma745xReadCal( mma10bit_t *cal);
  253. int Mma745xWriteCal( mma10bit_t *cal);
  254. /* Control Function */
  255. int Mma745xCtl( uint_fast8_t fkt, void *val);
  256. /* Startup Initialization */
  257. int Mma745xInit( uint_fast8_t selftest, mmaInit_t *init);
  258. #endif /* _DEV_MMA745X_H_ */