-
Notifications
You must be signed in to change notification settings - Fork 1
/
pru_spi.c
412 lines (294 loc) · 10.1 KB
/
pru_spi.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//----------------------------------------------------------------------
// spi_pru -- provides commands for spi transactions.
// This code is meant to run on the PRU.
// This is the bit-banging stuff which performs the SPI
// bus communication. It is meant to be called from a
// higher-level program.
//-----------------------------------------------------------------------
#include "pru_spi.h"
volatile register uint32_t __R30; // write reg
volatile register uint32_t __R31; // read reg
// This defines the positions of the SPI signals
// in the IO registers R30 and R31. The mapping
// of the reg bits to external pins on the device
// is done in the device tree overlay file.
#define CS 3 /* pr1_pru0_pru_r30_3 */
#define CLK 5 /* pr1_pru0_pru_r30_5 */
#define MOSI 1 /* pr1_pru0_pru_r30_1 */
#define MISO 2 /* pr1_pru0_pru_r31_2 */
// This defines the delay betwen transitions in the data bits.
#define DELAY_CNT 20
//================================================================
// Local fcns
//---------------------------------------------------------------
void wait_miso_high() {
uint8_t rx_bit;
while(1) {
rx_bit = (__R31>>MISO) & 0x01;
if (rx_bit) {
break;
}
}
}
//---------------------------------------------------------------
void wait_miso_low() {
uint8_t rx_bit;
while(1) {
rx_bit = (__R31>>MISO) & 0x01;
if (!rx_bit) {
break;
}
}
}
//================================================================
// Exported fcns.
//-----------------------------------------------------------------
void pru_spi_reset(void) {
// To reset the A/D, send SCK with MOSI high.
// Send at least 64 clocks with MOSI high. I
// send 72 clocks for good measure, but probably
// overkill.
int i, j;
uint8_t byte_cnt = 9;
// Set MOSI high
__R30 = __R30 | (1 << MOSI); // Transmit 1 bit
// Assert CS down
__R30 = __R30 & ~(1 << CS);
// Delay before sending bytes
__delay_cycles(DELAY_CNT);
// Loop over data bytes
for (i=0; i<byte_cnt; i++) {
// Loop over bits
for (j=0; j<8; j++) {
// set clk down
__R30 = __R30 & ~(1 << CLK); // Turn off clk
// Continue sending 1 bit.
__R30 = __R30 | (1 << MOSI); // Transmit 1 bit
// Delay
__delay_cycles(DELAY_CNT);
// Now set clk up
__R30 = __R30 | (1 << CLK); // Turn on clk
// Delay
__delay_cycles(DELAY_CNT);
} // loop over bits
// Quick delay before next byte
__delay_cycles(DELAY_CNT);
} // loop over bytes
// Bring CS back up at end of transaction
__R30 = __R30 | (1 << CS);
// Set MOSI low
__R30 = __R30 & ~(1 << MOSI); // Transmit 0 bit
}
//-----------------------------------------------------------------
void pru_spi_write(volatile uint32_t *pData, volatile int byte_cnt) {
// This performs a SPI write operation.
int i, j;
uint8_t data_byte;
// First assert CS down
__R30 = __R30 & ~(1 << CS);
// Delay before transmitting bytes
__delay_cycles(DELAY_CNT);
// Loop over data words *pData
for (i=0; i<byte_cnt; i++) {
data_byte = (uint8_t) *(pData+i);
// Loop over bits
for (j=0; j<8; j++) {
// set clk down
__R30 = __R30 & ~(1 << CLK); // Turn off clk
// Now set bit
if ( (data_byte<<j) & 0x80 ) {
// 1 bit
__R30 = __R30 | (1 << MOSI); // Turn on data bit
} else {
// 0 bit
__R30 = __R30 & ~(1 << MOSI); // Turn off data bit
}
// Delay to let bit settle
__delay_cycles(DELAY_CNT);
// Now set clk up
__R30 = __R30 | (1 << CLK); // Turn on clk
// Delay to let bit latch in
__delay_cycles(DELAY_CNT);
} // loop over bits
// Quick delay before next byte
__delay_cycles(DELAY_CNT);
} // loop over bytes
// Bring CS back up at end of transaction
__R30 = __R30 | (1 << CS);
// Set MOSI low when not transmitting data
__R30 = __R30 & ~(1 << MOSI); // Transmit 0 bit
return;
}
//---------------------------------------------------------------
uint8_t pru_spi_writeread_single(volatile uint32_t *pTxbuf, volatile int tx_cnt, uint32_t *pRxbuf, volatile int rx_cnt) {
// This first waits for MISO to go high, then low. Once MISO is low, this fcn
// performs a write, then keeps clocking in order to read and
// store the input data. It does not raise CS between the write and the read.
int i, j;
uint8_t data_byte;
uint8_t rx_bit, rx_byte;
uint8_t rx_word[4]; // Max wordsize is 32 bits.
uint32_t tmp;
// Set MOSI high
__R30 = __R30 | (1 << MOSI); // Turn on data bit
// First assert CS down
__R30 = __R30 & ~(1 << CS);
// set clk down
__R30 = __R30 & ~(1 << CLK); // Turn off clk
// Now wait for MISO to go high
// wait_miso_high();
// ----> Clock out Tx command from MOSI
// Loop over data words *pTxbuf
for (i=0; i<tx_cnt; i++) {
data_byte = (uint8_t) *(pTxbuf+i);
// Loop over bits in byte
for (j=0; j<8; j++) {
// set clk down
__R30 = __R30 & ~(1 << CLK); // Turn off clk
// Now set bit
if ( (data_byte<<j) & 0x80 ) {
// 1 bit
__R30 = __R30 | (1 << MOSI); // Turn on data bit
} else {
// 0 bit
__R30 = __R30 & ~(1 << MOSI); // Turn off data bit
}
// Delay to let bit settle
__delay_cycles(DELAY_CNT);
// Now set clk up
__R30 = __R30 | (1 << CLK); // Turn on clk
// Delay to let bit latch in
__delay_cycles(DELAY_CNT);
} // loop over bits
// Quick delay before next byte
__delay_cycles(DELAY_CNT);
} // loop over bytes
// Set MOSI low while clocking in reply
__R30 = __R30 & ~(1 << MOSI); // Transmit 0 bit
// ----> Now read incoming bytes from MISO
// Loop over data bytes
for (i=0; i<rx_cnt; i++) {
rx_bit = 0x00;
// Loop over bits
for (j=0; j<8; j++) {
// set clk down
__R30 = __R30 & ~(1 << CLK); // Turn off clk
// Delay to let things settle
__delay_cycles(DELAY_CNT);
// Now set clk up and read incoming MISO bit
__R30 = __R30 | (1 << CLK); // Turn on clk
rx_bit = (__R31>>MISO) & 0x01;
rx_byte = (rx_byte<<1) | rx_bit;
// Now wait for next 1/2 period.
__delay_cycles(DELAY_CNT);
} // loop over bits
// Must do some conversion since Rxbuf is a uint32_t word.
rx_word[i] = rx_byte;
// Quick delay before next byte
__delay_cycles(DELAY_CNT);
}
// Now convert rx_word to Rxbuf
tmp = 0x00;
for (i=0; i<rx_cnt; i++) {
tmp = ((tmp<<8) | rx_word[i]); // Shift in bytes
}
pRxbuf[0] = tmp;
// Bring CS back up at end of transaction
__R30 = __R30 | (1 << CS);
return 0x00;
}
//---------------------------------------------------------------
uint8_t pru_spi_writeread_continuous(uint32_t *pTxbuf, int tx_cnt, volatile uint32_t *pRxbuf, int rx_cnt, int ncnv) {
// This first waits for MISO to go high, then low. Once MISO is low, this fcn
// performs a write, then keeps clocking in order to read and
// store the input data. It does not raise CS until it has read all
// bytes requested.
// Inputs:
// pTxbuf = pointer to input buffer (cmd bytes)
// tx_cnt = number of bytes in cmd
// pRxbuf = pointer to output buffer (A/D code bytes)
// This buffer should be rx_cnt*ncnv bytes long.
// rx_cnt = number of bytes per A/D reading. Usually 3.
// ncnv = total number of A/D readings to make.
int ccnt;
int i, j;
uint8_t data_byte;
uint8_t rx_bit, rx_byte;
uint8_t rx_word[4]; // Max wordsize is 32 bits.
uint32_t tmp;
// Set MOSI high
__R30 = __R30 | (1 << MOSI); // Turn on data bit
// Next assert CS down
__R30 = __R30 & ~(1 << CS);
//**************************
// Now we enter big loop over A/D readings.
for (ccnt=0; ccnt<ncnv; ccnt++) {
wait_miso_high();
wait_miso_low();
// ----> Clock out Tx command from MOSI
// Loop over data words *pTxbuf
for (i=0; i<tx_cnt; i++) {
data_byte = (uint8_t) *(pTxbuf+i);
// Loop over bits
for (j=0; j<8; j++) {
// set clk down
__R30 = __R30 & ~(1 << CLK); // Turn off clk
// Now set bit
if ( (data_byte<<j) & 0x80 ) {
// 1 bit
__R30 = __R30 | (1 << MOSI); // Turn on data bit
} else {
// 0 bit
__R30 = __R30 & ~(1 << MOSI); // Turn off data bit
}
// Delay to let bit settle
__delay_cycles(DELAY_CNT);
// Now set clk up
__R30 = __R30 | (1 << CLK); // Turn on clk
// Delay to let bit latch in
__delay_cycles(DELAY_CNT);
} // loop over bits
// Quick delay before next byte
__delay_cycles(DELAY_CNT);
} // loop over bytes
// Set MOSI high while clocking in reply
__R30 = __R30 | (1 << MOSI); // Transmit 1 bit
// ----> Now read incoming bytes from MISO
// Loop over data bytes
for (i=0; i<rx_cnt; i++) {
rx_bit = 0x00;
// Loop over bits
for (j=0; j<8; j++) {
// set clk down
__R30 = __R30 & ~(1 << CLK); // Turn off clk
// Delay to let things settle
__delay_cycles(DELAY_CNT);
// Now set clk up and read incoming MISO bit
__R30 = __R30 | (1 << CLK); // Turn on clk
rx_bit = (__R31>>MISO) & 0x01;
rx_byte = (rx_byte<<1) | rx_bit;
// Now wait for next 1/2 period.
__delay_cycles(DELAY_CNT);
} // loop over bits
// Must do some conversion since Rxbuf is a uint32_t word.
rx_word[i] = rx_byte;
// Quick delay before next byte
__delay_cycles(DELAY_CNT);
} // Loop over data bytes i
// Now convert rx_word to Rxbuf
tmp = 0x00;
for (i=0; i<rx_cnt; i++) {
tmp = ((tmp<<8) | rx_word[i]); // Shift in bytes
}
pRxbuf[ccnt] = tmp;
// Wait a little bit until next loop.
__delay_cycles(10*DELAY_CNT);
//wait_miso_high();
} // for (ccnt=0; ccnt<ncnv; ccnt++) {
//**************************
// Bring CS back up at end of transaction
__R30 = __R30 | (1 << CS);
// Set MOSI low after transaction is over.
__R30 = __R30 & ~(1 << MOSI); // Transmit 0 bit
return 0x00;
}