Contiki 2.5
acc-adxl345.c
Go to the documentation of this file.
1 /* Copyright (c) 2010, Ulf Kulau
2  *
3  * Permission is hereby granted, free of charge, to any person
4  * obtaining a copy of this software and associated documentation
5  * files (the "Software"), to deal in the Software without
6  * restriction, including without limitation the rights to use,
7  * copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following
10  * conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \addtogroup Device Interfaces
27  * @{
28  *
29  * \addtogroup adxl345_interface
30  * @{
31  */
32 
33 /**
34  * \file
35  * ADXL345 Accelerometer interface implementation
36  * \author
37  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
38  */
39 #include "acc-adxl345.h"
40 
41 int8_t adxl345_init(void) {
42  uint8_t i = 0;
45 
49 
50  while (adxl345_read(ADXL345_DEVICE_ID_REG) != ADXL345_DEVICE_ID_DATA) {
51  _delay_ms(10);
52  if (i++ > 10) {
53  return -1;
54  }
55  }
56  return 0;
57 
58 }
59 
61  uint8_t byteLow;
62  uint8_t byteHigh;
63  byteLow = adxl345_read(ADXL345_OUTX_LOW_REG);
64  byteHigh = adxl345_read(ADXL345_OUTX_HIGH_REG);
65 
66  return ((byteHigh << 8) + byteLow);
67 }
68 
70  uint8_t byteLow;
71  uint8_t byteHigh;
72  byteLow = adxl345_read(ADXL345_OUTY_LOW_REG);
73  byteHigh = adxl345_read(ADXL345_OUTY_HIGH_REG);
74  return (byteHigh << 8) + byteLow;
75 }
76 
78  uint8_t byteLow;
79  uint8_t byteHigh;
80  byteLow = adxl345_read(ADXL345_OUTZ_LOW_REG);
81  byteHigh = adxl345_read(ADXL345_OUTZ_HIGH_REG);
82  return (byteHigh << 8) + byteLow;
83 }
84 
85 acc_data_t adxl345_get_acceleration(void) {
86  acc_data_t adxl345_data;
87  adxl345_data.acc_x_value = adxl345_get_x_acceleration();
88  adxl345_data.acc_y_value = adxl345_get_y_acceleration();
89  adxl345_data.acc_z_value = adxl345_get_z_acceleration();
90  return adxl345_data;
91 }
92 
93 void adxl345_write(uint8_t reg, uint8_t data) {
95  reg &= 0x7F;
96  mspi_transceive(reg);
97  mspi_transceive(data);
99 }
100 
101 uint8_t adxl345_read(uint8_t reg) {
102  uint8_t data;
104  reg |= 0x80;
105  mspi_transceive(reg);
106  data = mspi_transceive(MSPI_DUMMY_BYTE);
108  return data;
109 }
110