-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino.cpp
227 lines (174 loc) · 7.88 KB
/
arduino.cpp
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**********************************************************
SPI_Raspi_Arduino
Configures an Raspberry Pi as an SPI master and
demonstrates a basic bidirectional communication scheme
with an Arduino slave. The Raspberry Pi transmits
commands to perform addition and subtraction on a pair
of integers and the Ardunio returns the result
Compile String:
g++ -o SPI_Raspi_Arduino SPI_Raspi_Arduino.cpp
***********************************************************/
//#include <sys/ioctl.h>
////#include <linux/spi/spidev.h>
//#include <fcntl.h>
//#include <iostream>
//#include <cstring>
using namespace std;
/**********************************************************
Housekeeping variables
***********************************************************/
int results;
int fd;
/**********************************************************
Declare Functions
***********************************************************/
//int spiTxRx(unsigned char txDat);
//int sendCommand(char i, int j, int k);
/**********************************************************
Main
***********************************************************/
//int main (void)
//{
/**********************************************************
Setup SPI
Open file spidev0.0 (chip enable 0) for read/write access
with the file descriptor "fd"
Configure transfer speed (1MkHz)
***********************************************************/
//fd = open("/dev/spidev0.0", O_RDWR);
//unsigned int speed = 1000000;
//ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
/**********************************************************
An endless loop that repeatedly sends the demonstration
commands to the Arduino and displays the results
***********************************************************/
uint8_t arduino_buf1[5] = {69, 1 , 1, 1, 1};
uint8_t arduino_Rxbuf1[5] = {69,69,69,69, 69};
uint8_t arduino_buf2[5] = {45, 100, 25, 40, 10};
uint8_t arduino_Rxbuf2[5] = {69,69,69,69,69};
uint8_t y = 0;
while (y < 10)
{
results = SpiWriteAndRead(spi0, &arduino_buf1[0], &arduino_Rxbuf1[0], 5, false);
//results = sendCommand(69, 510, 655);
printf( "Addition results:\n");
printf("10, 8, 5, 4 = %d, %d, %d, %d, %d\n" , arduino_Rxbuf1[0], arduino_Rxbuf1[1], arduino_Rxbuf1[2],arduino_Rxbuf1[3], arduino_Rxbuf1[4]);
results = SpiWriteAndRead(spi0, &arduino_buf2[0], &arduino_Rxbuf2[0], 5, false);
//results = sendCommand(45, 1000, 250);
printf("Subtraction results:\n");
printf("100, 25, 40, 10 = %d, %d, %d, %d, %d\n\n", arduino_Rxbuf2[0], arduino_Rxbuf2[1], arduino_Rxbuf2[2], arduino_Rxbuf2[3], arduino_Rxbuf2[4]);
sleep(1);
y++;
}
/**********************************************************
spiTxRx
Transmits one byte via the SPI device, and returns one byte
as the result.
Establishes a data structure, spi_ioc_transfer as defined
by spidev.h and loads the various members to pass the data
and configuration parameters to the SPI device via IOCTL
Local variables txDat and rxDat are defined and passed by
reference.
***********************************************************/
/*
int spiTxRx(unsigned char txDat)
{
unsigned char rxDat;
struct spi_ioc_transfer spi;
memset (&spi, 0, sizeof (spi));
spi.tx_buf = (unsigned long)&txDat;
spi.rx_buf = (unsigned long)&rxDat;
spi.len = 1;
ioctl (fd, SPI_IOC_MESSAGE(1), &spi);
return rxDat;
}
*/
/**********************************************************
sendCommand
Demonstration of a protocol that uses the spiTxRx function
to send a formatted command sequence/packet to the Arduino
one byte at and capture the results
***********************************************************/
/*
int sendCommand(char command, int j, int k)
{
unsigned char resultByte;
bool ack;
*/
/**********************************************************
Unions allow variables to occupy the same memory space
a convenient way to move back and forth between 8-bit and
16-bit values etc.
Here three unions are declared: two for parameters to be
passed in commands to the Arduino and one to receive
the results
***********************************************************/
/*
union p1Buffer_T
{
int p1Int;
unsigned char p1Char [2];
} p1Buffer;
union p2Buffer_T
{
int p2Int;
unsigned char p2Char [2];
} p2Buffer;
union resultBuffer_T
{
int resultInt;
unsigned char resultChar [2];
} resultBuffer;
p1Buffer.p1Int = j;
p2Buffer.p2Int = k;
resultBuffer.resultInt = 0;
*/
/**********************************************************
An initial handshake sequence sends a one byte start code
('c') and loops endlessly until it receives the one byte
acknowledgment code ('a') and sets the ack flag to true.
(Note that the loop also sends the command byte while
still in handshake sequence to avoid wasting a transmit
cycle.)
***********************************************************/
/*
do
{
ack = false;
spiTxRx('c');
usleep (10);
resultByte = spiTxRx(command);
if (resultByte == 'a')
{
ack = true;
}
usleep (10);
}
while (ack == false);
*/
/**********************************************************
Send the parameters one byte at a time.
***********************************************************/
/*
spiTxRx(p1Buffer.p1Char[0]);
usleep (10);
spiTxRx(p1Buffer.p1Char[1]);
usleep (10);
spiTxRx(p2Buffer.p2Char[0]);
usleep (10);
spiTxRx(p2Buffer.p2Char[1]);
usleep (10);
*/
/**********************************************************
Push two more zeros through so the Arduino can return the
results
***********************************************************/
/*
resultByte = spiTxRx(0);
resultBuffer.resultChar[0] = resultByte;
usleep (10);
resultByte = spiTxRx(0);
resultBuffer.resultChar[1] = resultByte;
return resultBuffer.resultInt;
}
*/