-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPI_Arduino.ino
More file actions
156 lines (142 loc) · 4 KB
/
SPI_Arduino.ino
File metadata and controls
156 lines (142 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_LSM303.h"
int marker = 0;
unsigned char NOT_RDY = 'n';
unsigned char RDY = 'r';
unsigned char TXRX = 't';
unsigned char DONE = 'd';
volatile boolean isr_flag;
byte hold;
byte state;
int16_t data;
int acc_pin[3] = {0, 1, 2};
int16_t acc_data[3] = {0};
bool data_hold = false;
byte buf0;
byte buf1;
byte buf2;
byte buf3;
byte buf4;
byte buf5;
void setup(void) {
// Slave out
pinMode(MISO, OUTPUT);
// SPI -> slave mode
SPCR |= _BV(SPE);
// Activate interrupts
SPCR |= _BV(SPIE);
// Ready up the arduino for sensing
state = NOT_RDY;
SPDR = state;
// Accelerometer
pinMode(acc_pin[0], INPUT); pinMode(acc_pin[1], INPUT);
pinMode(acc_pin[2], INPUT);
}
ISR (SPI_STC_vect) {
uint8_t hold = SPDR;
// SPDR has changed!! Make sure SPDR = (state || data) depending on what is happening
switch(hold) {
case 'n':
// If rpi was not ready, change SPDR back to NOT_RDY
if (state == NOT_RDY) {
SPDR = state;
data_hold = false;
}
break;
case 'r':
// If arduino not ready, change SPDR = NOT_RDY
if (state == NOT_RDY) {
SPDR = state;
data_hold = false;
// If arduino is ready, set state to TXRX for data transmission
// Turn on data_hold to negate entry into data aquisition
} else if (state == RDY) {
state = TXRX;
SPDR = state;
data_hold = true;
// Otherwise, something probably went wrong, so start over by going into
// not ready state and set dala_hold = false ---> basically just forget
// the incident and take a new data set for the next attempt.
} else {
state = NOT_RDY;
SPDR = state;
data_hold = false;
}
break;
case 't':
// Transmit all bytes of data out if rpi and arduino are in txrx state
if (state == TXRX) {
switch(marker) {
case 0:
SPDR = buf0;
marker++;
break;
case 1:
SPDR = buf1;
marker++;
break;
case 2:
SPDR = buf2;
marker++;
break;
case 3:
SPDR = buf3;
marker++;
break;
case 4:
SPDR = buf4;
marker++;
break;
case 5:
SPDR = buf5;
marker++;
break;
case 6:
SPDR = DONE;
break;
}
} else {
state = NOT_RDY;
SPDR = state;
state = NOT_RDY;
SPDR = state;
data_hold = false;
}
break;
case 'd':
marker=0;
state = NOT_RDY;
SPDR = state;
data_hold = false;
break;
}
isr_flag = true;
}
void loop(void) {
if (isr_flag) {
isr_flag = false;
} else if ((!data_hold) && (state == NOT_RDY)) {
acc_read();
prep_data();
marker=0;
}
}
void acc_read() {
acc_data[0] = analogRead(acc_pin[0]);
acc_data[1] = analogRead(acc_pin[1]);
acc_data[2] = analogRead(acc_pin[2]);
}
void prep_data() {
data = (int16_t)acc_data[0];
buf0 = data & 0xFF;
buf1 = (data >> 8) & 0xFF;
data = (int16_t)acc_data[1];
buf2 = data & 0xFF;
buf3 = (data >> 8) & 0xFF;
data = (int16_t)acc_data[2];
buf4 = data & 0xFF;
buf5 = (data >> 8) & 0xFF;
state = RDY;
SPDR = state;
}