Skip to content

Commit 6c48249

Browse files
committed
Created Spark library
1 parent 9813c28 commit 6c48249

File tree

6 files changed

+467
-0
lines changed

6 files changed

+467
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Joe Goggins
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
ADXL362
2+
=======
3+
4+
A library for manipulating ADXL362 accelerometer and temperature sensor for the Spark Core and Spark Internet Button v1.0.
5+
Implementation based on the ADXL362 library for Arduino, https://github.com/annem/ADXL362 by Anne Mahaffey and Jonathan Ruiz de Garibay, ported to Spark by Anthony Liekens.
6+
7+
Components Required
8+
---
9+
- Spark core
10+
- Example built with the Spark Internet Button v1.0 or ADXL362 connected to the Spark PCI bus
11+
12+
Example Usage
13+
---
14+
15+
See this [example](firmware/examples/ADXL362_SimpleRead.cpp) for details.

firmware/ADXL362.cpp

+294
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
/*
2+
Arduino Library for Analog Devices ADXL362 - Micropower 3-axis accelerometer for Spark Core and Spark Internet Button v1.0
3+
go to http://www.analog.com/ADXL362 for datasheet
4+
5+
6+
License: CC BY-SA 3.0: Creative Commons Share-alike 3.0. Feel free
7+
to use and abuse this code however you'd like. If you find it useful
8+
please attribute, and SHARE-ALIKE!
9+
10+
Created June 2012
11+
by Anne Mahaffey - hosted on http://annem.github.com/ADXL362
12+
13+
Modified May 2013
14+
by Jonathan Ruiz de Garibay
15+
16+
Ported to Spark October 2014
17+
by Anthony Liekens
18+
19+
*/
20+
21+
#include "application.h"
22+
#include "ADXL362.h"
23+
24+
//#define ADXL362_DEBUG
25+
26+
int16_t slaveSelectPin = 10;
27+
28+
ADXL362::ADXL362() {
29+
}
30+
31+
32+
//
33+
// begin()
34+
// Initial SPI setup, soft reset of device
35+
//
36+
void ADXL362::begin(int16_t chipSelectPin) {
37+
slaveSelectPin = chipSelectPin;
38+
pinMode(slaveSelectPin, OUTPUT);
39+
SPI.begin();
40+
SPI.setDataMode(SPI_MODE0); //CPHA = CPOL = 0 MODE = 0
41+
delay(1000);
42+
43+
// soft reset
44+
SPIwriteOneRegister(0x1F, 0x52); // Write to SOFT RESET, "R"
45+
delay(10);
46+
#ifdef ADXL362_DEBUG
47+
Serial.println("Soft Reset\n");
48+
#endif
49+
}
50+
51+
//
52+
// beginMeasure()
53+
// turn on Measurement mode - required after reset
54+
//
55+
void ADXL362::beginMeasure() {
56+
byte temp = SPIreadOneRegister(0x2D); // read Reg 2D before modifying for measure mode
57+
58+
#ifdef ADXL362_DEBUG
59+
Serial.print( "Setting Measeurement Mode - Reg 2D before = ");
60+
Serial.print(temp);
61+
#endif
62+
63+
// turn on measurement mode
64+
byte tempwrite = temp | 0x02; // turn on measurement bit in Reg 2D
65+
SPIwriteOneRegister(0x2D, tempwrite); // Write to POWER_CTL_REG, Measurement Mode
66+
delay(10);
67+
68+
#ifdef ADXL362_DEBUG
69+
temp = SPIreadOneRegister(0x2D);
70+
Serial.print( ", Reg 2D after = ");
71+
Serial.println(temp);
72+
Serial.println();
73+
#endif
74+
}
75+
76+
//
77+
// readXData(), readYData(), readZData(), readTemp()
78+
// Read X, Y, Z, and Temp registers
79+
//
80+
int16_t ADXL362::readXData(){
81+
int16_t XDATA = SPIreadTwoRegisters(0x0E);
82+
83+
#ifdef ADXL362_DEBUG
84+
Serial.print("XDATA = ");
85+
Serial.println(XDATA);
86+
#endif
87+
88+
return XDATA;
89+
}
90+
91+
int16_t ADXL362::readYData(){
92+
int16_t YDATA = SPIreadTwoRegisters(0x10);
93+
94+
#ifdef ADXL362_DEBUG
95+
Serial.print("\tYDATA = ");
96+
Serial.println(YDATA);
97+
#endif
98+
99+
return YDATA;
100+
}
101+
102+
int16_t ADXL362::readZData(){
103+
int16_t ZDATA = SPIreadTwoRegisters(0x12);
104+
105+
#ifdef ADXL362_DEBUG
106+
Serial.print("\tZDATA = ");
107+
Serial.println(ZDATA);
108+
#endif
109+
110+
return ZDATA;
111+
}
112+
113+
int16_t ADXL362::readTemp(){
114+
int16_t TEMP = SPIreadTwoRegisters(0x14);
115+
116+
#ifdef ADXL362_DEBUG
117+
Serial.print("\tTEMP = ");
118+
Serial.println(TEMP);
119+
#endif
120+
121+
return TEMP;
122+
}
123+
124+
void ADXL362::readXYZTData(int16_t &XData, int16_t &YData, int16_t &ZData, int16_t &Temperature){
125+
// burst SPI read
126+
// A burst read of all three axis is required to guarantee all measurements correspond to same sample time
127+
digitalWrite(slaveSelectPin, LOW);
128+
SPI.transfer(0x0B); // read instruction
129+
SPI.transfer(0x0E); // Start at XData Reg
130+
XData = SPI.transfer(0x00);
131+
XData = XData + (SPI.transfer(0x00) << 8);
132+
YData = SPI.transfer(0x00);
133+
YData = YData + (SPI.transfer(0x00) << 8);
134+
ZData = SPI.transfer(0x00);
135+
ZData = ZData + (SPI.transfer(0x00) << 8);
136+
Temperature = SPI.transfer(0x00);
137+
Temperature = Temperature + (SPI.transfer(0x00) << 8);
138+
digitalWrite(slaveSelectPin, HIGH);
139+
140+
#ifdef ADXL362_DEBUG
141+
Serial.print("XDATA = "); Serial.print(XData);
142+
Serial.print("\tYDATA = "); Serial.print(YData);
143+
Serial.print("\tZDATA = "); Serial.print(ZData);
144+
Serial.print("\tTemperature = "); Serial.println(Temperature);
145+
#endif
146+
}
147+
148+
void ADXL362::setupDCActivityInterrupt(int16_t threshold, byte time){
149+
// Setup motion and time thresholds
150+
SPIwriteTwoRegisters(0x20, threshold);
151+
SPIwriteOneRegister(0x22, time);
152+
153+
// turn on activity interrupt
154+
byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Read current reg value
155+
ACT_INACT_CTL_Reg = ACT_INACT_CTL_Reg | (0x01); // turn on bit 1, ACT_EN
156+
SPIwriteOneRegister(0x27, ACT_INACT_CTL_Reg); // Write new reg value
157+
ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Verify properly written
158+
159+
#ifdef ADXL362_DEBUG
160+
Serial.print("DC Activity Threshold set to "); Serial.print(SPIreadTwoRegisters(0x20));
161+
Serial.print(", Time threshold set to "); Serial.print(SPIreadOneRegister(0x22));
162+
Serial.print(", ACT_INACT_CTL Register is "); Serial.println(ACT_INACT_CTL_Reg, HEX);
163+
#endif
164+
}
165+
166+
void ADXL362::setupACActivityInterrupt(int16_t threshold, byte time){
167+
// Setup motion and time thresholds
168+
SPIwriteTwoRegisters(0x20, threshold);
169+
SPIwriteOneRegister(0x22, time);
170+
171+
// turn on activity interrupt
172+
byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Read current reg value
173+
ACT_INACT_CTL_Reg = ACT_INACT_CTL_Reg | (0x03); // turn on bit 2 and 1, ACT_AC_DCB, ACT_EN
174+
SPIwriteOneRegister(0x27, ACT_INACT_CTL_Reg); // Write new reg value
175+
ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Verify properly written
176+
177+
#ifdef ADXL362_DEBUG
178+
Serial.print("AC Activity Threshold set to "); Serial.print(SPIreadTwoRegisters(0x20));
179+
Serial.print(", Time Activity set to "); Serial.print(SPIreadOneRegister(0x22));
180+
Serial.print(", ACT_INACT_CTL Register is "); Serial.println(ACT_INACT_CTL_Reg, HEX);
181+
#endif
182+
}
183+
184+
void ADXL362::setupDCInactivityInterrupt(int16_t threshold, int16_t time){
185+
// Setup motion and time thresholds
186+
SPIwriteTwoRegisters(0x23, threshold);
187+
SPIwriteTwoRegisters(0x25, time);
188+
189+
// turn on inactivity interrupt
190+
byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Read current reg value
191+
ACT_INACT_CTL_Reg = ACT_INACT_CTL_Reg | (0x04); // turn on bit 3, INACT_EN
192+
SPIwriteOneRegister(0x27, ACT_INACT_CTL_Reg); // Write new reg value
193+
ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Verify properly written
194+
195+
#ifdef ADXL362_DEBUG
196+
Serial.print("DC Inactivity Threshold set to "); Serial.print(SPIreadTwoRegisters(0x23));
197+
Serial.print(", Time Inactivity set to "); Serial.print(SPIreadTwoRegisters(0x25));
198+
Serial.print(", ACT_INACT_CTL Register is "); Serial.println(ACT_INACT_CTL_Reg, HEX);
199+
#endif
200+
}
201+
202+
void ADXL362::setupACInactivityInterrupt(int16_t threshold, int16_t time){
203+
// Setup motion and time thresholds
204+
SPIwriteTwoRegisters(0x23, threshold);
205+
SPIwriteTwoRegisters(0x25, time);
206+
207+
// turn on inactivity interrupt
208+
byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Read current reg value
209+
ACT_INACT_CTL_Reg = ACT_INACT_CTL_Reg | (0x0C); // turn on bit 3 and 4, INACT_AC_DCB, INACT_EN
210+
SPIwriteOneRegister(0x27, ACT_INACT_CTL_Reg); // Write new reg value
211+
ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); // Verify properly written
212+
213+
#ifdef ADXL362_DEBUG
214+
Serial.print("AC Inactivity Threshold set to "); Serial.print(SPIreadTwoRegisters(0x23));
215+
Serial.print(", Time Inactivity set to "); Serial.print(SPIreadTwoRegisters(0x25));
216+
Serial.print(", ACT_INACT_CTL Register is "); Serial.println(ACT_INACT_CTL_Reg, HEX);
217+
#endif
218+
}
219+
220+
void ADXL362::checkAllControlRegs(){
221+
//byte filterCntlReg = SPIreadOneRegister(0x2C);
222+
//byte ODR = filterCntlReg & 0x07; Serial.print("ODR = "); Serial.println(ODR, HEX);
223+
//byte ACT_INACT_CTL_Reg = SPIreadOneRegister(0x27); Serial.print("ACT_INACT_CTL_Reg = "); Serial.println(ACT_INACT_CTL_Reg, HEX);
224+
digitalWrite(slaveSelectPin, LOW);
225+
SPI.transfer(0x0B); // read instruction
226+
SPI.transfer(0x20); // Start burst read at Reg 20
227+
#ifdef ADXL362_DEBUG
228+
Serial.println("Start Burst Read of all Control Regs - Library version 6-5-2014:");
229+
Serial.print("Reg 20 = "); Serial.println(SPI.transfer(0x00), HEX);
230+
Serial.print("Reg 21 = "); Serial.println(SPI.transfer(0x00), HEX);
231+
Serial.print("Reg 22 = "); Serial.println(SPI.transfer(0x00), HEX);
232+
Serial.print("Reg 23 = "); Serial.println(SPI.transfer(0x00), HEX);
233+
Serial.print("Reg 24 = "); Serial.println(SPI.transfer(0x00), HEX);
234+
Serial.print("Reg 25 = "); Serial.println(SPI.transfer(0x00), HEX);
235+
Serial.print("Reg 26 = "); Serial.println(SPI.transfer(0x00), HEX);
236+
Serial.print("Reg 27 = "); Serial.println(SPI.transfer(0x00), HEX);
237+
Serial.print("Reg 28 = "); Serial.println(SPI.transfer(0x00), HEX);
238+
Serial.print("Reg 29 = "); Serial.println(SPI.transfer(0x00), HEX);
239+
Serial.print("Reg 2A = "); Serial.println(SPI.transfer(0x00), HEX);
240+
Serial.print("Reg 2B = "); Serial.println(SPI.transfer(0x00), HEX);
241+
Serial.print("Reg 2C = "); Serial.println(SPI.transfer(0x00), HEX);
242+
Serial.print("Reg 2D = "); Serial.println(SPI.transfer(0x00), HEX);
243+
Serial.print("Reg 2E = "); Serial.println(SPI.transfer(0x00), HEX);
244+
#endif
245+
digitalWrite(slaveSelectPin, HIGH);
246+
}
247+
248+
// Basic SPI routines to simplify code
249+
// read and write one register
250+
byte ADXL362::SPIreadOneRegister(byte regAddress){
251+
byte regValue = 0;
252+
253+
digitalWrite(slaveSelectPin, LOW);
254+
SPI.transfer(0x0B); // read instruction
255+
SPI.transfer(regAddress);
256+
regValue = SPI.transfer(0x00);
257+
digitalWrite(slaveSelectPin, HIGH);
258+
259+
return regValue;
260+
}
261+
262+
void ADXL362::SPIwriteOneRegister(byte regAddress, byte regValue){
263+
264+
digitalWrite(slaveSelectPin, LOW);
265+
SPI.transfer(0x0A); // write instruction
266+
SPI.transfer(regAddress);
267+
SPI.transfer(regValue);
268+
digitalWrite(slaveSelectPin, HIGH);
269+
}
270+
271+
int16_t ADXL362::SPIreadTwoRegisters(byte regAddress){
272+
int16_t twoRegValue = 0;
273+
274+
digitalWrite(slaveSelectPin, LOW);
275+
SPI.transfer(0x0B); // read instruction
276+
SPI.transfer(regAddress);
277+
twoRegValue = SPI.transfer(0x00);
278+
twoRegValue = twoRegValue + (SPI.transfer(0x00) << 8);
279+
digitalWrite(slaveSelectPin, HIGH);
280+
281+
return twoRegValue;
282+
}
283+
284+
void ADXL362::SPIwriteTwoRegisters(byte regAddress, int16_t twoRegValue){
285+
byte twoRegValueH = twoRegValue >> 8;
286+
byte twoRegValueL = twoRegValue;
287+
288+
digitalWrite(slaveSelectPin, LOW);
289+
SPI.transfer(0x0A); // write instruction
290+
SPI.transfer(regAddress);
291+
SPI.transfer(twoRegValueL);
292+
SPI.transfer(twoRegValueH);
293+
digitalWrite(slaveSelectPin, HIGH);
294+
}

0 commit comments

Comments
 (0)