Contiki 2.5
Main Page
Related Pages
Modules
Data Structures
Files
Examples
File List
Globals
platform
inga
interfaces
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;
43
mspi_chip_release
(
ADXL345_CS
);
44
mspi_init
(
ADXL345_CS
,
MSPI_MODE_3
,
MSPI_BAUD_MAX
);
45
46
adxl345_write
(
ADXL345_DATA_FORMAT_REG
,
ADXL345_DATA_FORMAT_DATA
);
47
adxl345_write
(
ADXL345_POWER_CTL_REG
,
ADXL345_POWER_CTL_DATA
);
48
adxl345_write
(
ADXL345_BW_RATE_REG
,
ADXL345_BW_RATE_DATA
);
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
60
uint16_t
adxl345_get_x_acceleration
(
void
) {
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
69
uint16_t
adxl345_get_y_acceleration
(
void
) {
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
77
uint16_t
adxl345_get_z_acceleration
(
void
) {
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) {
94
mspi_chip_select
(
ADXL345_CS
);
95
reg &= 0x7F;
96
mspi_transceive
(reg);
97
mspi_transceive
(data);
98
mspi_chip_release
(
ADXL345_CS
);
99
}
100
101
uint8_t
adxl345_read
(uint8_t reg) {
102
uint8_t data;
103
mspi_chip_select
(
ADXL345_CS
);
104
reg |= 0x80;
105
mspi_transceive
(reg);
106
data =
mspi_transceive
(MSPI_DUMMY_BYTE);
107
mspi_chip_release
(
ADXL345_CS
);
108
return
data;
109
}
110
Generated on Fri Aug 30 2013 12:34:11 for Contiki 2.5 by
1.8.3.1