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.cpp 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. /**
  35. * Includes
  36. */
  37. #include "ADXL345.h"
  38. ADXL345::ADXL345(PinName mosi,
  39. PinName miso,
  40. PinName sck,
  41. PinName cs) : spi_(mosi, miso, sck), nCS_(cs) {
  42. //2MHz, allowing us to use the fastest data rates.
  43. spi_.frequency(2000000);
  44. spi_.format(8,3);
  45. nCS_ = 1;
  46. wait_us(500);
  47. }
  48. int ADXL345::getDevId(void) {
  49. return oneByteRead(ADXL345_DEVID_REG);
  50. }
  51. int ADXL345::getTapThreshold(void) {
  52. return oneByteRead(ADXL345_THRESH_TAP_REG);
  53. }
  54. void ADXL345::setTapThreshold(int threshold) {
  55. oneByteWrite(ADXL345_THRESH_TAP_REG, threshold);
  56. }
  57. int ADXL345::getOffset(int axis) {
  58. int address = 0;
  59. if (axis == ADXL345_X) {
  60. address = ADXL345_OFSX_REG;
  61. } else if (axis == ADXL345_Y) {
  62. address = ADXL345_OFSY_REG;
  63. } else if (axis == ADXL345_Z) {
  64. address = ADXL345_OFSZ_REG;
  65. }
  66. return oneByteRead(address);
  67. }
  68. void ADXL345::setOffset(int axis, char offset) {
  69. int address = 0;
  70. if (axis == ADXL345_X) {
  71. address = ADXL345_OFSX_REG;
  72. } else if (axis == ADXL345_Y) {
  73. address = ADXL345_OFSY_REG;
  74. } else if (axis == ADXL345_Z) {
  75. address = ADXL345_OFSZ_REG;
  76. }
  77. return oneByteWrite(address, offset);
  78. }
  79. int ADXL345::getTapDuration(void) {
  80. return oneByteRead(ADXL345_DUR_REG)*625;
  81. }
  82. void ADXL345::setTapDuration(int duration_us) {
  83. int tapDuration = duration_us / 625;
  84. oneByteWrite(ADXL345_DUR_REG, tapDuration);
  85. }
  86. float ADXL345::getTapLatency(void) {
  87. return oneByteRead(ADXL345_LATENT_REG)*1.25;
  88. }
  89. void ADXL345::setTapLatency(int latency_ms) {
  90. int tapLatency = latency_ms / 1.25;
  91. oneByteWrite(ADXL345_LATENT_REG, tapLatency);
  92. }
  93. float ADXL345::getWindowTime(void) {
  94. return oneByteRead(ADXL345_WINDOW_REG)*1.25;
  95. }
  96. void ADXL345::setWindowTime(int window_ms) {
  97. int windowTime = window_ms / 1.25;
  98. oneByteWrite(ADXL345_WINDOW_REG, windowTime);
  99. }
  100. int ADXL345::getActivityThreshold(void) {
  101. return oneByteRead(ADXL345_THRESH_ACT_REG);
  102. }
  103. void ADXL345::setActivityThreshold(int threshold) {
  104. oneByteWrite(ADXL345_THRESH_ACT_REG, threshold);
  105. }
  106. int ADXL345::getInactivityThreshold(void) {
  107. return oneByteRead(ADXL345_THRESH_INACT_REG);
  108. }
  109. void ADXL345::setInactivityThreshold(int threshold) {
  110. return oneByteWrite(ADXL345_THRESH_INACT_REG, threshold);
  111. }
  112. int ADXL345::getTimeInactivity(void) {
  113. return oneByteRead(ADXL345_TIME_INACT_REG);
  114. }
  115. void ADXL345::setTimeInactivity(int timeInactivity) {
  116. oneByteWrite(ADXL345_TIME_INACT_REG, timeInactivity);
  117. }
  118. int ADXL345::getActivityInactivityControl(void) {
  119. return oneByteRead(ADXL345_ACT_INACT_CTL_REG);
  120. }
  121. void ADXL345::setActivityInactivityControl(int settings) {
  122. oneByteWrite(ADXL345_ACT_INACT_CTL_REG, settings);
  123. }
  124. int ADXL345::getFreefallThreshold(void) {
  125. return oneByteRead(ADXL345_THRESH_FF_REG);
  126. }
  127. void ADXL345::setFreefallThreshold(int threshold) {
  128. oneByteWrite(ADXL345_THRESH_FF_REG, threshold);
  129. }
  130. int ADXL345::getFreefallTime(void) {
  131. return oneByteRead(ADXL345_TIME_FF_REG)*5;
  132. }
  133. void ADXL345::setFreefallTime(int freefallTime_ms) {
  134. int freefallTime = freefallTime_ms / 5;
  135. oneByteWrite(ADXL345_TIME_FF_REG, freefallTime);
  136. }
  137. int ADXL345::getTapAxisControl(void) {
  138. return oneByteRead(ADXL345_TAP_AXES_REG);
  139. }
  140. void ADXL345::setTapAxisControl(int settings) {
  141. oneByteWrite(ADXL345_TAP_AXES_REG, settings);
  142. }
  143. int ADXL345::getTapSource(void) {
  144. return oneByteRead(ADXL345_ACT_TAP_STATUS_REG);
  145. }
  146. void ADXL345::setPowerMode(char mode) {
  147. //Get the current register contents, so we don't clobber the rate value.
  148. char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
  149. registerContents = (mode << 4) | registerContents;
  150. oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
  151. }
  152. int ADXL345::getPowerControl(void) {
  153. return oneByteRead(ADXL345_POWER_CTL_REG);
  154. }
  155. void ADXL345::setPowerControl(int settings) {
  156. oneByteWrite(ADXL345_POWER_CTL_REG, settings);
  157. }
  158. int ADXL345::getInterruptEnableControl(void) {
  159. return oneByteRead(ADXL345_INT_ENABLE_REG);
  160. }
  161. void ADXL345::setInterruptEnableControl(int settings) {
  162. oneByteWrite(ADXL345_INT_ENABLE_REG, settings);
  163. }
  164. int ADXL345::getInterruptMappingControl(void) {
  165. return oneByteRead(ADXL345_INT_MAP_REG);
  166. }
  167. void ADXL345::setInterruptMappingControl(int settings) {
  168. oneByteWrite(ADXL345_INT_MAP_REG, settings);
  169. }
  170. int ADXL345::getInterruptSource(void){
  171. return oneByteRead(ADXL345_INT_SOURCE_REG);
  172. }
  173. int ADXL345::getDataFormatControl(void){
  174. return oneByteRead(ADXL345_DATA_FORMAT_REG);
  175. }
  176. void ADXL345::setDataFormatControl(int settings){
  177. oneByteWrite(ADXL345_DATA_FORMAT_REG, settings);
  178. }
  179. void ADXL345::setDataRate(int rate) {
  180. //Get the current register contents, so we don't clobber the power bit.
  181. char registerContents = oneByteRead(ADXL345_BW_RATE_REG);
  182. registerContents &= 0x10;
  183. registerContents |= rate;
  184. oneByteWrite(ADXL345_BW_RATE_REG, registerContents);
  185. }
  186. void ADXL345::getOutput(int* readings){
  187. char buffer[6];
  188. multiByteRead(ADXL345_DATAX0_REG, buffer, 6);
  189. readings[0] = (int)buffer[1] << 8 | (int)buffer[0];
  190. readings[1] = (int)buffer[3] << 8 | (int)buffer[2];
  191. readings[2] = (int)buffer[5] << 8 | (int)buffer[4];
  192. }
  193. int ADXL345::getFifoControl(void){
  194. return oneByteRead(ADXL345_FIFO_CTL);
  195. }
  196. void ADXL345::setFifoControl(int settings){
  197. oneByteWrite(ADXL345_FIFO_STATUS, settings);
  198. }
  199. int ADXL345::getFifoStatus(void){
  200. return oneByteRead(ADXL345_FIFO_STATUS);
  201. }
  202. int ADXL345::oneByteRead(int address) {
  203. int tx = (ADXL345_SPI_READ | (address & 0x3F));
  204. int rx = 0;
  205. nCS_ = 0;
  206. //Send address to read from.
  207. spi_.write(tx);
  208. //Read back contents of address.
  209. rx = spi_.write(0x00);
  210. nCS_ = 1;
  211. return rx;
  212. }
  213. void ADXL345::oneByteWrite(int address, char data) {
  214. int tx = (ADXL345_SPI_WRITE | (address & 0x3F));
  215. nCS_ = 0;
  216. //Send address to write to.
  217. spi_.write(tx);
  218. //Send data to be written.
  219. spi_.write(data);
  220. nCS_ = 1;
  221. }
  222. void ADXL345::multiByteRead(int startAddress, char* buffer, int size) {
  223. int tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
  224. nCS_ = 0;
  225. //Send address to start reading from.
  226. spi_.write(tx);
  227. for (int i = 0; i < size; i++) {
  228. buffer[i] = spi_.write(0x00);
  229. }
  230. nCS_ = 1;
  231. }
  232. void ADXL345::multiByteWrite(int startAddress, char* buffer, int size) {
  233. int tx = (ADXL345_SPI_WRITE | ADXL345_MULTI_BYTE | (startAddress & 0x3F));
  234. nCS_ = 0;
  235. //Send address to start reading from.
  236. spi_.write(tx);
  237. for (int i = 0; i < size; i++) {
  238. buffer[i] = spi_.write(0x00);
  239. }
  240. nCS_ = 1;
  241. }