forked from gicking/stm8gal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi_Arduino_comm.c
219 lines (160 loc) · 6.55 KB
/
spi_Arduino_comm.c
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
/**
\file spi_Arduino_comm.c
\author G. Icking-Konert
\date 2017-12-19
\version 0.1
\brief implementation of SPI routines voa Arduino USB<->SPI bridge
implementation of routines for SPI communication using the Arduino
USB<->SPI bridge available from https://github.com/gicking/Arduino_SPI_bridge
*/
// include files
#include "spi_Arduino_comm.h"
#include "main.h"
#include "serial_comm.h"
#include "misc.h"
/**
\fn uint8_t checksum_Arduino(uint8_t *buf)
\param[in] buf buffer containing frame. buf[0] contains total frame length
Calculate checksum over frame (inverted XOR over bytes w/o checksum).
*/
uint8_t checksum_Arduino(uint8_t *buf) {
uint8_t chk = 0xFF, i;
uint8_t lim = buf[0]-1; // assert only one minus
for (i=0; i<lim; i++)
chk ^= buf[i];
return(chk);
} // checksum_Arduino()
/**
\fn void configSPI_Arduino(HANDLE fp, uint32_t baudrateSPI)
\param[in] fp handle to Arduino port
\param[in] baudrateSPI SPI baudrate
\param[in] bitOrder SPI bit order (LSB/MSB)
\param[in] mode SPI polarity & clock phase (see Arduino SPI reference)
configure SPI interface of Arduino SPI bridge
For serial protocol see https://github.com/gicking/Arduino_SPI_bridge/protocol.ods
*/
void configSPI_Arduino(HANDLE fp, uint32_t baudrateSPI, uint8_t bitOrder, uint8_t mode) {
uint8_t Tx[150], Rx[150];
uint8_t lenRx, num;
// assemble frame
Tx[0] = 9; // frame length
Tx[1] = ARDUINO_CMD_CONFIG_SPI; // command code
Tx[2] = (uint8_t) (baudrateSPI >> 24); // baudrate MSB
Tx[3] = (uint8_t) (baudrateSPI >> 16);
Tx[4] = (uint8_t) (baudrateSPI >> 8);
Tx[5] = (uint8_t) (baudrateSPI >> 0); // ...LSB
Tx[6] = bitOrder; // SPI bit order
Tx[7] = mode; // polarity & clock phase
Tx[8] = checksum_Arduino(Tx); // frame checksum
// send command to Arduino
send_port(fp, 0, Tx[0], (char*) Tx);
// get response from Arduino
lenRx = 3;
num = receive_port(fp, 0, lenRx, (char*) Rx);
// check for timeout
if (num != lenRx)
Error("in 'configSPI_Arduino()': response timeout");
// check length
if (Rx[0] != lenRx)
Error("in 'configSPI_Arduino()': wrong frame length (expect %d, read %d)", (int) lenRx, (int) (Rx[0]));
// check checksum
if (Rx[lenRx-1] != checksum_Arduino(Rx))
Error("in 'configSPI_Arduino()': checksum error (expect 0x%02x, read 0x%02x)", Rx[lenRx-1], checksum_Arduino(Rx));
// check for ACK
if (Rx[1] != ARDUINO_SUCCESS)
Error("in 'configSPI_Arduino()': acknowledge error (expect 0x%02x, read 0x%02x)", ARDUINO_SUCCESS, Rx[1]);
} // configSPI_Arduino
/**
\fn void setPin_Arduino(HANDLE fp, uint8_t pin, bool state)
\param[in] fp handle to Arduino port
\param[in] pin Arduino pin to set
\param[in] state state for pin
set pin on Arduino SPI bridge using serial command.
For serial protocol see https://github.com/gicking/Arduino_SPI_bridge/protocol.ods
*/
void setPin_Arduino(HANDLE fp, uint8_t pin, uint8_t state) {
uint8_t Tx[150], Rx[150];
uint8_t lenRx, num;
// assemble frame
Tx[0] = 5; // frame length
Tx[1] = ARDUINO_CMD_SET_PIN; // command code
Tx[2] = pin; // pin number
Tx[3] = state; // new state
Tx[4] = checksum_Arduino(Tx); // frame checksum
// send command to Arduino
send_port(fp, 0, Tx[0], (char*) Tx);
// get response from Arduino
lenRx = 3;
num = receive_port(fp, 0, lenRx, (char*) Rx);
// check for timeout
if (num != lenRx)
Error("in 'setPin_Arduino()': response timeout");
// check length
if (Rx[0] != lenRx)
Error("in 'setPin_Arduino()': wrong frame length (expect %d, read %d)", (int) lenRx, (int) (Rx[0]));
// check checksum
if (Rx[lenRx-1] != checksum_Arduino(Rx))
Error("in 'setPin_Arduino()': checksum error (expect 0x%02x, read 0x%02x)", Rx[lenRx-1], checksum_Arduino(Rx));
// check for ACK
if (Rx[1] != ARDUINO_SUCCESS)
Error("in 'setPin_Arduino()': acknowledge error (expect 0x%02x, read 0x%02x)", ARDUINO_SUCCESS, Rx[1]);
} // setPin_Arduino
/**
\fn uint32_t sendReceiveSPI_Arduino(HANDLE fpSPI, uint8_t CSN, uint32_t lenFrame, char *Tx, char *Rx)
\param[in] fp handle to Arduino port
\param[in] CSN Arduino pin used as chip-select
\param[in] lenFrame number of SPI bytes to send
\param[in] bufTx array of SPI bytes to send
\param[out] bufRx array of received SPI bytes
\return number of received SPI bytes
send/receive SPI frames via Arduino USB<->SPI bridge
For serial protocol see https://github.com/gicking/Arduino_SPI_bridge/protocol.ods
*/
uint32_t sendReceiveSPI_Arduino(HANDLE fp, uint8_t CSN, uint32_t lenFrame, char *bufTx, char *bufRx) {
uint8_t Tx[1000], Rx[1000];
uint8_t lenRx, num;
int i;
// assemble frame
Tx[0] = 4+lenFrame; // frame length
Tx[1] = ARDUINO_CMD_SEND_RECEIVE; // command code
Tx[2] = CSN; // chip select pin
for (i=0; i<lenFrame; i++) { // copy MOSI bytes
if (bufTx != NULL)
Tx[3+i] = bufTx[i];
else
Tx[3+i] = 0x00;
}
Tx[Tx[0]-1] = checksum_Arduino(Tx); // frame checksum
// send command to Arduino
send_port(fp, 0, Tx[0], (char*) Tx);
// get response from Arduino
lenRx = 3+lenFrame;
num = receive_port(fp, 0, lenRx, (char*) Rx);
// check for timeout
if (num != lenRx)
Error("in 'sendReceiveSPI_Arduino()': response timeout");
// check length
if (Rx[0] != lenRx)
Error("in 'sendReceiveSPI_Arduino()': wrong frame length (expect %d, read %d)", (int) lenRx, (int) (Rx[0]));
// check checksum
if (Rx[lenRx-1] != checksum_Arduino(Rx))
Error("in 'sendReceiveSPI_Arduino()': checksum error (expect 0x%02x, read 0x%02x)", Rx[lenRx-1], checksum_Arduino(Rx));
// check for ACK
if (Rx[1] != ARDUINO_SUCCESS)
Error("in 'sendReceiveSPI_Arduino()': acknowledge error (expect 0x%02x, read 0x%02x)", ARDUINO_SUCCESS, Rx[1]);
// copy MISO bytes
if (bufRx != NULL) {
for (i=0; i<lenFrame; i++)
bufRx[i] = Rx[2+i];
}
// debug SPI data
#if defined(DEBUG)
fprintf(stderr, "n MOSI MISO\n");
for (i=0; i<lenFrame; i++)
fprintf(stderr, "%d 0x%02x 0x%02x\n", i, Tx[3+i], Rx[2+i]);
fprintf(stderr, "\n");
#endif
// return number of SPI bytes
return(lenFrame);
} // sendReceiveSPI_Arduino
// end of file