Subversion Repositories Code-Repo

Rev

Rev 251 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
248 Kevin 1
#include "adxl345.h"
2
#include "hal_hardware_board.h"
3
#include "hal_SPI.h"
4
 
5
/*
6
 * Writes the given byte to the register at the address
7
 */
8
void WriteSPI(unsigned char addr, unsigned char data) {
251 Kevin 9
	halSPISetPolarityPhase(1,1);		// ADXL345 operates in SPI(1,1)
248 Kevin 10
	ADXL_CS_LOW();
11
	spiSendByte(addr & ~ADXL_BYTE_WRITE);
12
	spiSendByte(data);
13
	ADXL_CS_HIGH();
251 Kevin 14
	halSPISetPolarityPhase(0,0);		// Revert back to SPI(0,0)
248 Kevin 15
}
16
 
17
/*
18
 * Reads the value of the register at the address
19
 */
20
unsigned char ReadSPI(unsigned char addr) {
21
	unsigned char ret;
251 Kevin 22
	halSPISetPolarityPhase(1,1);		// ADXL345 operates in SPI(1,1)
248 Kevin 23
	ADXL_CS_LOW();
24
	spiSendByte(addr | ADXL_BYTE_READ);
25
	spiReadFrame(&ret, 1);
26
	ADXL_CS_HIGH();
251 Kevin 27
	halSPISetPolarityPhase(0,0);		// Revert back to SPI(0,0)
248 Kevin 28
	return ret;
29
}
30
 
31
/*
32
 * Reads a 16-bit value of the register at the address
33
 */
34
int Read16SPI(unsigned char addr) {
35
	unsigned char recv[2];
36
	int ret;
251 Kevin 37
	halSPISetPolarityPhase(1,1);		// ADXL345 operates in SPI(1,1)
248 Kevin 38
	ADXL_CS_LOW();
39
	spiSendByte(addr | ADXL_MULTI_BYTE_READ);
40
	spiReadFrame(recv, 2);
41
	ADXL_CS_HIGH();
251 Kevin 42
	halSPISetPolarityPhase(0,0);		// Revert back to SPI(0,0)
248 Kevin 43
	ret = recv[0];
44
	ret |= recv[1] << 8;
45
	return ret;
46
}
47
 
48
/*
49
 * ADXL345 Initialization Function
50
 */
51
unsigned char ADXLInit(void) {
52
	unsigned char deviceID;
53
 
54
	// Check if device exists
55
	deviceID = ReadSPI(ADXL345_REG_DEVID);
56
	if (deviceID != 0xE5)
57
		return 1;
58
 
59
	// Set FIFO to bypass mode
60
	WriteSPI(ADXL345_REG_FIFO_CTL, 0x00);
61
 
62
//	// Set the INT_INVERT bit in DATA_FORMAT register
63
	WriteSPI(ADXL345_REG_DATA_FORMAT, 0x20);
64
 
65
	ADXLSetRange(ADXL345_RANGE_16_G);
66
 
67
	ADXLSetDataRate(ADXL345_DATARATE_12_5_HZ);
68
 
69
	ADXLClearInterrupts();
70
 
71
	ADXLStandbyOff();
72
 
73
	return 0;
74
}
75
 
76
void ADXLStandbyOn(void) {
77
	// Turn off measurements
78
	WriteSPI(ADXL345_REG_POWER_CTL, 0x00);
79
}
80
 
81
void ADXLStandbyOff(void) {
82
	// Turn on measurements
83
	WriteSPI(ADXL345_REG_POWER_CTL, 0x08);
84
}
85
 
86
/*
87
 * Sets up and enables the tap interrupt
88
 */
89
void ADXLInitInterrupts(void) {
90
	// Set tap acceleration threshold  (62.5mg/LSB, 0xFF=16g)
251 Kevin 91
	WriteSPI(ADXL345_REG_THRESH_TAP, 0x18);
248 Kevin 92
 
93
	// Set maximum tap duration (625us/LSB)
94
	WriteSPI(ADXL345_REG_DUR, 0xFF);
95
 
96
	// Set minimum time for second tap (1.25ms/LSB)
97
	WriteSPI(ADXL345_REG_LATENT, 0xFF);
98
 
99
	// Set maximum time for second tap (1.25ms/LSB)
100
	WriteSPI(ADXL345_REG_WINDOW, 0xFF);
101
 
102
	// Bind single tap to INT1 and double tap to INT2
103
	WriteSPI(ADXL345_REG_INT_MAP, 0x20);
104
 
105
	// Enable all three axis for tap detection
106
	WriteSPI(ADXL345_REG_TAP_AXES, 0x07);
107
 
108
	// Enable single tap interrupts
109
	WriteSPI(ADXL345_REG_INT_ENABLE, 0x40);
110
}
111
 
112
/*
113
 * Clears any active interrupt flags
114
 */
115
void ADXLClearInterrupts(void) {
251 Kevin 116
	ReadSPI(ADXL345_REG_ACT_TAP_STATUS);
248 Kevin 117
	ReadSPI(ADXL345_REG_INT_SOURCE);
118
}
119
 
120
/*
121
 * Returns sensor data
122
 */
123
void ADXLRead(int *x, int *y, int *z) {
124
	*x = Read16SPI(ADXL345_REG_DATAX0);
125
	*y = Read16SPI(ADXL345_REG_DATAY0);
126
	*z = Read16SPI(ADXL345_REG_DATAZ0);
127
}
128
 
129
/*
130
 * Sets the sensitivity range (2/4/8/16g)
131
 */
132
void ADXLSetRange(range_t range) {
133
	unsigned char data = ReadSPI(ADXL345_REG_DATA_FORMAT);
134
 
135
	// Set the new sensitivity range
136
	data &= ~0x0F;
137
	data |= range;
138
 
139
	// Set the FULL_RES bit
140
	data |= 0x08;
141
 
142
	WriteSPI(ADXL345_REG_DATA_FORMAT, data);
143
}
144
 
145
/*
146
 * Sets the data rate
147
 */
148
void ADXLSetDataRate(dataRate_t rate) {
149
	unsigned char data = rate;
150
 
151
#ifdef ADXL345_LOW_POWER_MODE
152
	// Set the low power mode bit
153
	data |= 0x10;
154
#endif
155
 
156
	WriteSPI(ADXL345_REG_BW_RATE, data);
157
}