Keyboard firmwares for Atmel AVR and Cortex-M
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.

ADXL345.h 16KB

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