You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

576 lines
18 KiB

8 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /**
  2. * @author Peter Swanson
  3. * A personal note from me: Jesus Christ has changed my life so much it blows my mind. I say this because
  4. * today, religion is thought of as something that you do or believe and has about as
  5. * little impact on a person as their political stance. But for me, God gives me daily
  6. * strength and has filled my life with the satisfaction that I could never find in any
  7. * of the other things that I once looked for it in.
  8. * If your interested, heres verse that changed my life:
  9. * Rom 8:1-3: "Therefore, there is now no condemnation for those who are in Christ Jesus,
  10. * because through Christ Jesus, the law of the Spirit who gives life has set
  11. * me free from the law of sin (which brings...) and death. For what the law
  12. * was powerless to do in that it was weakened by the flesh, God did by sending
  13. * His own Son in the likeness of sinful flesh to be a sin offering. And so He
  14. * condemned sin in the flesh in order that the righteous requirements of the
  15. * (God's) law might be fully met in us, who live not according to the flesh
  16. * but according to the Spirit."
  17. *
  18. * A special thanks to Ewout van Bekkum for all his patient help in developing this library!
  19. *
  20. * @section LICENSE
  21. *
  22. * Permission is hereby granted, free of charge, to any person obtaining a copy
  23. * of this software and associated documentation files (the "Software"), to deal
  24. * in the Software without restriction, including without limitation the rights
  25. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  26. * copies of the Software, and to permit persons to whom the Software is
  27. * furnished to do so, subject to the following conditions:
  28. *
  29. * The above copyright notice and this permission notice shall be included in
  30. * all copies or substantial portions of the Software.
  31. *
  32. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  33. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  34. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  35. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  36. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  37. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  38. * THE SOFTWARE.
  39. *
  40. * @section DESCRIPTION
  41. *
  42. * ADXL345, triple axis, I2C interface, accelerometer.
  43. *
  44. * Datasheet:
  45. *
  46. * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
  47. */
  48. #ifndef ADXL345_I2C_H
  49. #define ADXL345_I2C_H
  50. /**
  51. * Includes
  52. */
  53. #include "mbed.h"
  54. /**
  55. * Defines
  56. */
  57. //Registers.
  58. #define ADXL345_DEVID_REG 0x00
  59. #define ADXL345_THRESH_TAP_REG 0x1D
  60. #define ADXL345_OFSX_REG 0x1E
  61. #define ADXL345_OFSY_REG 0x1F
  62. #define ADXL345_OFSZ_REG 0x20
  63. #define ADXL345_DUR_REG 0x21
  64. #define ADXL345_LATENT_REG 0x22
  65. #define ADXL345_WINDOW_REG 0x23
  66. #define ADXL345_THRESH_ACT_REG 0x24
  67. #define ADXL345_THRESH_INACT_REG 0x25
  68. #define ADXL345_TIME_INACT_REG 0x26
  69. #define ADXL345_ACT_INACT_CTL_REG 0x27
  70. #define ADXL345_THRESH_FF_REG 0x28
  71. #define ADXL345_TIME_FF_REG 0x29
  72. #define ADXL345_TAP_AXES_REG 0x2A
  73. #define ADXL345_ACT_TAP_STATUS_REG 0x2B
  74. #define ADXL345_BW_RATE_REG 0x2C
  75. #define ADXL345_POWER_CTL_REG 0x2D
  76. #define ADXL345_INT_ENABLE_REG 0x2E
  77. #define ADXL345_INT_MAP_REG 0x2F
  78. #define ADXL345_INT_SOURCE_REG 0x30
  79. #define ADXL345_DATA_FORMAT_REG 0x31
  80. #define ADXL345_DATAX0_REG 0x32
  81. #define ADXL345_DATAX1_REG 0x33
  82. #define ADXL345_DATAY0_REG 0x34
  83. #define ADXL345_DATAY1_REG 0x35
  84. #define ADXL345_DATAZ0_REG 0x36
  85. #define ADXL345_DATAZ1_REG 0x37
  86. #define ADXL345_FIFO_CTL 0x38
  87. #define ADXL345_FIFO_STATUS 0x39
  88. //Data rate codes.
  89. #define ADXL345_3200HZ 0x0F
  90. #define ADXL345_1600HZ 0x0E
  91. #define ADXL345_800HZ 0x0D
  92. #define ADXL345_400HZ 0x0C
  93. #define ADXL345_200HZ 0x0B
  94. #define ADXL345_100HZ 0x0A
  95. #define ADXL345_50HZ 0x09
  96. #define ADXL345_25HZ 0x08
  97. #define ADXL345_12HZ5 0x07
  98. #define ADXL345_6HZ25 0x06
  99. // read or write bytes
  100. #define ADXL345_I2C_READ 0xA7
  101. #define ADXL345_I2C_WRITE 0xA6
  102. #define ADXL345_I2C_ADDRESS 0x53 //the ADXL345 7-bit address is 0x53 when ALT ADDRESS is low as it is on the sparkfun chip: when ALT ADDRESS is high the address is 0x1D
  103. /////////////when ALT ADDRESS pin is high:
  104. //#define ADXL345_I2C_READ 0x3B
  105. //#define ADXL345_I2C_WRITE 0x3A
  106. //#define ADXL345_I2C_ADDRESS 0x1D
  107. #define ADXL345_X 0x00
  108. #define ADXL345_Y 0x01
  109. #define ADXL345_Z 0x02
  110. // modes
  111. #define MeasurementMode 0x08
  112. class ADXL345_I2C {
  113. public:
  114. /**
  115. * Constructor.
  116. *
  117. * @param mosi mbed pin to use for SDA line of I2C interface.
  118. * @param sck mbed pin to use for SCL line of I2C interface.
  119. */
  120. ADXL345_I2C(PinName sda, PinName scl);
  121. /**
  122. * Get the output of all three axes.
  123. *
  124. * @param Pointer to a buffer to hold the accelerometer value for the
  125. * x-axis, y-axis and z-axis [in that order].
  126. */
  127. void getOutput(int* readings);
  128. /**
  129. * Read the device ID register on the device.
  130. *
  131. * @return The device ID code [0xE5]
  132. */
  133. char getDeviceID(void);
  134. /**
  135. * Set the power mode.
  136. *
  137. * @param mode 0 -> Normal operation.
  138. * 1 -> Reduced power operation.
  139. */
  140. int setPowerMode(char mode);
  141. /**
  142. * Set the power control settings.
  143. *
  144. * See datasheet for details.
  145. *
  146. * @param The control byte to write to the POWER_CTL register.
  147. */
  148. int setPowerControl(char settings);
  149. /**
  150. * Get the power control settings.
  151. *
  152. * See datasheet for details.
  153. *
  154. * @return The contents of the POWER_CTL register.
  155. */
  156. char getPowerControl(void);
  157. /**
  158. * Get the data format settings.
  159. *
  160. * @return The contents of the DATA_FORMAT register.
  161. */
  162. char getDataFormatControl(void);
  163. /**
  164. * Set the data format settings.
  165. *
  166. * @param settings The control byte to write to the DATA_FORMAT register.
  167. */
  168. int setDataFormatControl(char settings);
  169. /**
  170. * Set the data rate.
  171. *
  172. * @param rate The rate code (see #defines or datasheet).
  173. */
  174. int setDataRate(char rate);
  175. /**
  176. * Get the current offset for a particular axis.
  177. *
  178. * @param axis 0x00 -> X-axis
  179. * 0x01 -> Y-axis
  180. * 0x02 -> Z-axis
  181. * @return The current offset as an 8-bit 2's complement number with scale
  182. * factor 15.6mg/LSB.
  183. */
  184. char getOffset(char axis);
  185. /**
  186. * Set the offset for a particular axis.
  187. *
  188. * @param axis 0x00 -> X-axis
  189. * 0x01 -> Y-axis
  190. * 0x02 -> Z-axis
  191. * @param offset The offset as an 8-bit 2's complement number with scale
  192. * factor 15.6mg/LSB.
  193. */
  194. int setOffset(char axis, char offset);
  195. /**
  196. * Get the FIFO control settings.
  197. *
  198. * @return The contents of the FIFO_CTL register.
  199. */
  200. char getFifoControl(void);
  201. /**
  202. * Set the FIFO control settings.
  203. *
  204. * @param The control byte to write to the FIFO_CTL register.
  205. */
  206. int setFifoControl(char settings);
  207. /**
  208. * Get FIFO status.
  209. *
  210. * @return The contents of the FIFO_STATUS register.
  211. */
  212. char getFifoStatus(void);
  213. /**
  214. * Read the tap threshold on the device.
  215. *
  216. * @return The tap threshold as an 8-bit number with a scale factor of
  217. * 62.5mg/LSB.
  218. */
  219. char getTapThreshold(void);
  220. /**
  221. * Set the tap threshold.
  222. *
  223. * @param The tap threshold as an 8-bit number with a scale factor of
  224. * 62.5mg/LSB.
  225. */
  226. int setTapThreshold(char threshold);
  227. /**
  228. * Get the tap duration required to trigger an event.
  229. *
  230. * @return The max time that an event must be above the tap threshold to
  231. * qualify as a tap event, in microseconds.
  232. */
  233. float getTapDuration(void);
  234. /**
  235. * Set the tap duration required to trigger an event.
  236. *
  237. * @param duration_us The max time that an event must be above the tap
  238. * threshold to qualify as a tap event, in microseconds.
  239. * Time will be normalized by the scale factor which is
  240. * 625us/LSB. A value of 0 disables the single/double
  241. * tap functions.
  242. */
  243. int setTapDuration(short int duration_us);
  244. /**
  245. * Get the tap latency between the detection of a tap and the time window.
  246. *
  247. * @return The wait time from the detection of a tap event to the start of
  248. * the time window during which a possible second tap event can be
  249. * detected in milliseconds.
  250. */
  251. float getTapLatency(void);
  252. /**
  253. * Set the tap latency between the detection of a tap and the time window.
  254. *
  255. * @param latency_ms The wait time from the detection of a tap event to the
  256. * start of the time window during which a possible
  257. * second tap event can be detected in milliseconds.
  258. * A value of 0 disables the double tap function.
  259. */
  260. int setTapLatency(short int latency_ms);
  261. /**
  262. * Get the time of window between tap latency and a double tap.
  263. *
  264. * @return The amount of time after the expiration of the latency time
  265. * during which a second valid tap can begin, in milliseconds.
  266. */
  267. float getWindowTime(void);
  268. /**
  269. * Set the time of the window between tap latency and a double tap.
  270. *
  271. * @param window_ms The amount of time after the expiration of the latency
  272. * time during which a second valid tap can begin,
  273. * in milliseconds.
  274. */
  275. int setWindowTime(short int window_ms);
  276. /**
  277. * Get the threshold value for detecting activity.
  278. *
  279. * @return The threshold value for detecting activity as an 8-bit number.
  280. * Scale factor is 62.5mg/LSB.
  281. */
  282. char getActivityThreshold(void);
  283. /**
  284. * Set the threshold value for detecting activity.
  285. *
  286. * @param threshold The threshold value for detecting activity as an 8-bit
  287. * number. Scale factor is 62.5mg/LSB. A value of 0 may
  288. * result in undesirable behavior if the activity
  289. * interrupt is enabled.
  290. */
  291. int setActivityThreshold(char threshold);
  292. /**
  293. * Get the threshold value for detecting inactivity.
  294. *
  295. * @return The threshold value for detecting inactivity as an 8-bit number.
  296. * Scale factor is 62.5mg/LSB.
  297. */
  298. char getInactivityThreshold(void);
  299. /**
  300. * Set the threshold value for detecting inactivity.
  301. *
  302. * @param threshold The threshold value for detecting inactivity as an
  303. * 8-bit number. Scale factor is 62.5mg/LSB.
  304. */
  305. int setInactivityThreshold(char threshold);
  306. /**
  307. * Get the time required for inactivity to be declared.
  308. *
  309. * @return The amount of time that acceleration must be less than the
  310. * inactivity threshold for inactivity to be declared, in
  311. * seconds.
  312. */
  313. char getTimeInactivity(void);
  314. /**
  315. * Set the time required for inactivity to be declared.
  316. *
  317. * @param inactivity The amount of time that acceleration must be less than
  318. * the inactivity threshold for inactivity to be
  319. * declared, in seconds. A value of 0 results in an
  320. * interrupt when the output data is less than the
  321. * threshold inactivity.
  322. */
  323. int setTimeInactivity(char timeInactivity);
  324. /**
  325. * Get the activity/inactivity control settings.
  326. *
  327. * D7 D6 D5 D4
  328. * +-----------+--------------+--------------+--------------+
  329. * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable |
  330. * +-----------+--------------+--------------+--------------+
  331. *
  332. * D3 D2 D1 D0
  333. * +-------------+----------------+----------------+----------------+
  334. * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable |
  335. * +-------------+----------------+----------------+----------------+
  336. *
  337. * See datasheet for details.
  338. *
  339. * @return The contents of the ACT_INACT_CTL register.
  340. */
  341. char getActivityInactivityControl(void);
  342. /**
  343. * Set the activity/inactivity control settings.
  344. *
  345. * D7 D6 D5 D4
  346. * +-----------+--------------+--------------+--------------+
  347. * | ACT ac/dc | ACT_X enable | ACT_Y enable | ACT_Z enable |
  348. * +-----------+--------------+--------------+--------------+
  349. *
  350. * D3 D2 D1 D0
  351. * +-------------+----------------+----------------+----------------+
  352. * | INACT ac/dc | INACT_X enable | INACT_Y enable | INACT_Z enable |
  353. * +-------------+----------------+----------------+----------------+
  354. *
  355. * See datasheet for details.
  356. *
  357. * @param settings The control byte to write to the ACT_INACT_CTL register.
  358. */
  359. int setActivityInactivityControl(char settings);
  360. /**
  361. * Get the threshold for free fall detection.
  362. *
  363. * @return The threshold value for free-fall detection, as an 8-bit number,
  364. * with scale factor 62.5mg/LSB.
  365. */
  366. char getFreefallThreshold(void);
  367. /**
  368. * Set the threshold for free fall detection.
  369. *
  370. * @return The threshold value for free-fall detection, as an 8-bit number,
  371. * with scale factor 62.5mg/LSB. A value of 0 may result in
  372. * undesirable behavior if the free-fall interrupt is enabled.
  373. * Values between 300 mg and 600 mg (0x05 to 0x09) are recommended.
  374. */
  375. int setFreefallThreshold(char threshold);
  376. /**
  377. * Get the time required to generate a free fall interrupt.
  378. *
  379. * @return The minimum time that the value of all axes must be less than
  380. * the freefall threshold to generate a free-fall interrupt, in
  381. * milliseconds.
  382. */
  383. char getFreefallTime(void);
  384. /**
  385. * Set the time required to generate a free fall interrupt.
  386. *
  387. * @return The minimum time that the value of all axes must be less than
  388. * the freefall threshold to generate a free-fall interrupt, in
  389. * milliseconds. A value of 0 may result in undesirable behavior
  390. * if the free-fall interrupt is enabled. Values between 100 ms
  391. * and 350 ms (0x14 to 0x46) are recommended.
  392. */
  393. int setFreefallTime(short int freefallTime_ms);
  394. /**
  395. * Get the axis tap settings.
  396. *
  397. * D3 D2 D1 D0
  398. * +----------+--------------+--------------+--------------+
  399. * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable |
  400. * +----------+--------------+--------------+--------------+
  401. *
  402. * (D7-D4 are 0s).
  403. *
  404. * See datasheet for more details.
  405. *
  406. * @return The contents of the TAP_AXES register.
  407. */
  408. char getTapAxisControl(void);
  409. /**
  410. * Set the axis tap settings.
  411. *
  412. * D3 D2 D1 D0
  413. * +----------+--------------+--------------+--------------+
  414. * | Suppress | TAP_X enable | TAP_Y enable | TAP_Z enable |
  415. * +----------+--------------+--------------+--------------+
  416. *
  417. * (D7-D4 are 0s).
  418. *
  419. * See datasheet for more details.
  420. *
  421. * @param The control byte to write to the TAP_AXES register.
  422. */
  423. int setTapAxisControl(char settings);
  424. /**
  425. * Get the source of a tap.
  426. *
  427. * @return The contents of the ACT_TAP_STATUS register.
  428. */
  429. char getTapSource(void);
  430. /**
  431. * Get the interrupt enable settings.
  432. *
  433. * @return The contents of the INT_ENABLE register.
  434. */
  435. char getInterruptEnableControl(void);
  436. /**
  437. * Set the interrupt enable settings.
  438. *
  439. * @param settings The control byte to write to the INT_ENABLE register.
  440. */
  441. int setInterruptEnableControl(char settings);
  442. /**
  443. * Get the interrupt mapping settings.
  444. *
  445. * @return The contents of the INT_MAP register.
  446. */
  447. char getInterruptMappingControl(void);
  448. /**
  449. * Set the interrupt mapping settings.
  450. *
  451. * @param settings The control byte to write to the INT_MAP register.
  452. */
  453. int setInterruptMappingControl(char settings);
  454. /**
  455. * Get the interrupt source.
  456. *
  457. * @return The contents of the INT_SOURCE register.
  458. */
  459. char getInterruptSource(void);
  460. private:
  461. I2C i2c_;
  462. /**
  463. * Read one byte from a register on the device.
  464. *
  465. * @param: - the address to be read from
  466. *
  467. * @return: the value of the data read
  468. */
  469. char SingleByteRead(char address);
  470. /**
  471. * Write one byte to a register on the device.
  472. *
  473. * @param:
  474. - address of the register to write to.
  475. - the value of the data to store
  476. */
  477. int SingleByteWrite(char address, char data);
  478. /**
  479. * Read several consecutive bytes on the device and store them in a given location.
  480. *
  481. * @param startAddress: The address of the first register to read from.
  482. * @param ptr_output: a pointer to the location to store the data being read
  483. * @param size: The number of bytes to read.
  484. */
  485. void multiByteRead(char startAddress, char* ptr_output, int size);
  486. /**
  487. * Write several consecutive bytes on the device.
  488. *
  489. * @param startAddress: The address of the first register to write to.
  490. * @param ptr_data: Pointer to a location which contains the data to write.
  491. * @param size: The number of bytes to write.
  492. */
  493. int multiByteWrite(char startAddress, char* ptr_data, int size);
  494. };
  495. #endif /* ADXL345_I2C_H */