Contiki-Inga 3.x
adxl345.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * ADXL345 Accelerometer interface implementation
33  * \author
34  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
35  * Enrico Jörns <joerns@ibr.cs.tu-bs.de>
36  */
37 
38 /**
39  * \addtogroup adxl345_interface
40  * @{
41  */
42 
43 #include "adxl345.h"
44 
45 #define DEBUG 1
46 
47 #if DEBUG
48 #include <stdio.h>
49 #define PRINTF(...) printf(__VA_ARGS__)
50 #else
51 #define PRINTF(...)
52 #endif
53 
54 #define ADXL345_DEVICE_ID_DATA 0xE5
55 /*----------------------------------------------------------------------------*/
56 int8_t
58 {
59  uint8_t i = 0;
62 
63  while (adxl345_read(ADXL345_DEVICE_ID_REG) != ADXL345_DEVICE_ID_DATA) {
64  _delay_ms(10);
65  if (i++ > 10) {
66  return 0;
67  }
68  }
69  PRINTF("adxl345: is available\n");
70  return 1;
71 }
72 /*----------------------------------------------------------------------------*/
73 int8_t
75 {
76 
77  if (!adxl345_available()) {
78  return -1;
79  }
80  // full resolution, 2g range
82  // measuring enabled
84  // output data rate: 100Hz
85  adxl345_write(ADXL345_BW_RATE_REG, ADXL345_ODR_100HZ);
86 
87  PRINTF("adxl345: initialized\n");
88 
89  return 0;
90 }
91 /*----------------------------------------------------------------------------*/
92 void
94 {
95  adxl345_set_powermode(ADXL345_PMODE_STANDBY);
96 }
97 
98 /*----------------------------------------------------------------------------*/
99 void
100 adxl345_set_g_range(uint8_t range)
101 {
102  uint8_t tmp_reg = adxl345_read(ADXL345_DATA_FORMAT_REG);
103  adxl345_write(ADXL345_DATA_FORMAT_REG, (tmp_reg & 0xFC) | (range & 0x03));
104 }
105 /*----------------------------------------------------------------------------*/
106 void
108 {
109  uint8_t tmp_reg = adxl345_read(ADXL345_BW_RATE_REG);
110  adxl345_write(ADXL345_BW_RATE_REG, (tmp_reg & 0xF0) | (rate & 0x0F));
111 }
112 /*----------------------------------------------------------------------------*/
113 void
114 adxl345_set_fifomode(uint8_t mode)
115 {
116  uint8_t tmp_reg = adxl345_read(ADXL345_FIFO_CTL_REG);
117  adxl345_write(ADXL345_FIFO_CTL_REG, (tmp_reg & 0x3F) | (mode & 0xC0));
118 }
119 /*----------------------------------------------------------------------------*/
120 void
122 {
123  uint8_t tmp_reg = adxl345_read(ADXL345_POWER_CTL_REG);
124  switch (mode) {
125  // sleep mode with 1Hz readings
126  case ADXL345_PMODE_SLEEP:
127  tmp_reg &= 0xF8;
128  tmp_reg |= (1 << ADXL345_SLEEP) | (0x3 << ADXL345_WAKEUP_L);
129  break;
130  case ADXL345_PMODE_WAKEUP:
131  tmp_reg &= 0xF9;
132  tmp_reg |= (1 << ADXL345_MEASURE);
133  tmp_reg &= ~(1 << ADXL345_SLEEP);
134  break;
135  case ADXL345_PMODE_STANDBY:
136  tmp_reg &= ~(1 << ADXL345_MEASURE);
137  break;
138  }
140 }
141 /*----------------------------------------------------------------------------*/
142 uint8_t
144 {
146 }
147 /*----------------------------------------------------------------------------*/
148 int16_t
150 {
151  uint8_t byteLow;
152  uint8_t byteHigh;
155 
156  return ((byteHigh << 8) + byteLow);
157 }
158 /*----------------------------------------------------------------------------*/
159 int16_t
161 {
162  uint8_t byteLow;
163  uint8_t byteHigh;
166  return (byteHigh << 8) +byteLow;
167 }
168 /*----------------------------------------------------------------------------*/
169 int16_t
171 {
172  uint8_t byteLow;
173  uint8_t byteHigh;
176  return (byteHigh << 8) +byteLow;
177 }
178 /*----------------------------------------------------------------------------*/
179 acc_data_t
181 {
182  acc_data_t adxl345_data;
183  uint8_t lsb = 0, msb = 0;
185  mspi_transceive(ADXL345_OUTX_LOW_REG | 0xC0); // read, multiple
186  lsb = mspi_transceive(MSPI_DUMMY_BYTE);
187  msb = mspi_transceive(MSPI_DUMMY_BYTE);
188  adxl345_data.x = (int16_t) ((msb << 8) + lsb);
189  lsb = mspi_transceive(MSPI_DUMMY_BYTE);
190  msb = mspi_transceive(MSPI_DUMMY_BYTE);
191  adxl345_data.y = (int16_t) ((msb << 8) + lsb);
192  lsb = mspi_transceive(MSPI_DUMMY_BYTE);
193  msb = mspi_transceive(MSPI_DUMMY_BYTE);
194  adxl345_data.z = (int16_t) ((msb << 8) + lsb);
196  PRINTF("adxl345: x: %d, y: %d, z: %d\n", adxl345_data.x, adxl345_data.y, adxl345_data.z);
197  return adxl345_data;
198 }
199 /*----------------------------------------------------------------------------*/
200 int8_t
201 adxl345_get_acceleration_fifo(acc_data_t* ret)
202 {
203  uint8_t idx = 0;
204  uint8_t fifolevel = adxl345_read(ADXL345_FIFO_STATUS_REG) & 0x3F;
205 
206  for (idx = 0; idx < fifolevel; idx++) {
207  ret[idx] = adxl345_get();
208  }
209 
210  return fifolevel;
211 }
212 /*----------------------------------------------------------------------------*/
213 void
214 adxl345_write(uint8_t reg, uint8_t data)
215 {
217  reg &= 0x7F;
218  mspi_transceive(reg);
219  mspi_transceive(data);
221 }
222 /*----------------------------------------------------------------------------*/
223 uint8_t
224 adxl345_read(uint8_t reg)
225 {
226  uint8_t data;
228  reg |= 0x80;
229  mspi_transceive(reg);
230  data = mspi_transceive(MSPI_DUMMY_BYTE);
232  return data;
233 }
234 /*----------------------------------------------------------------------------*/