-
Notifications
You must be signed in to change notification settings - Fork 1
/
spidriver_host.c
432 lines (347 loc) · 12 KB
/
spidriver_host.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//----------------------------------------------------------------------
// spidriver_host -- Host-side A/D wrapper which sends commands to PRUs.
// This code is meant to run on the host (ARM). The goal of the wrapping
// is to deal with low-level spi issues like inserting the bytecount and flag
// at the beginning of the message, init of PRU communication, etc.
//
//-----------------------------------------------------------------------
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <prussdrv.h>
#include <pruss_intc_mapping.h>
#include "spidriver_host.h"
#include "pru_spi.h"
#define PRU0 0
#define PRU1 1
//=====================================================================
uint8_t pruss_init(void) {
// This initializes the PRUSS driver stuff, and sets up the
// host side of the link. The code is not specific to one
// PRU or the other -- it is about configuring the PRUSS stuff
// on the host.
uint8_t retval = 0;
tpruss_intc_initdata pruss_intc_initdata = PRUSS_INTC_INITDATA;
retval |= prussdrv_init ();
retval |= prussdrv_open (PRU_EVTOUT_0);
retval |= prussdrv_pruintc_init(&pruss_intc_initdata);
//printf("Just ran pruss_init, retval = %d\n", retval);
return retval;
}
//-------------------------------------------------------------------
uint8_t pru0_init(void) {
// This initializes PRU0 -- the spi communication link. It
// zeros out the communication memory, then loads and runs the
// PRU0 program.
uint8_t retval = 0;
// printf("===> Entered pru0_init.\n");
// Get pointer to PRU0 dataram.
// printf("Asking for pointer to PRU0 dataram.\n");
retval = prussdrv_map_prumem(PRUSS0_PRU0_DATARAM, (void **) &pru0_dataram);
if (retval != 0) {
printf("prussdrv_map_prumem PRUSS0_PRU0_DATARAM map failed\n");
exit(-1);
}
// printf("Asking for pointer to PRU0 dataram.\n");
prussdrv_pru_reset(PRU0);
// Now run program on PRU0
// printf("Now trying to start program on PRU0.\n");
retval = prussdrv_exec_program (PRU0, "./text0.bin");
if (retval != 0) {
printf("prussdrv_exec_program(PRU0) failed\n");
exit(-1);
}
usleep(500);
// printf("<=== Done. Now leaving pru0_init.\n");
return 0;
}
//-------------------------------------------------------------------
uint8_t pru1_init(void) {
// This initializes PRU1 -- the spi communication link. It
// zeros out the communication memory, then loads and runs the
// PRU0 program.
uint8_t retval = 0;
#if 0
// Get pointer to PRU0 dataram.
retval = prussdrv_map_prumem(PRUSS0_PRU1_DATARAM, (void **) &pru1_dataram);
if (retval != 0) {
printf("prussdrv_map_prumem PRUSS0_PRU1_DATARAM map failed\n");
exit(-1);
}
#endif
prussdrv_pru_reset(PRU1);
#if 0
// Now run program on PRU1
retval = prussdrv_exec_program (PRU1, "./text1.bin");
if (retval != 0) {
printf("prussdrv_exec_program(PRU1) failed\n");
exit(-1);
}
#endif
usleep(500);
return 0;
}
//--------------------------------------------------------------
uint8_t pru_reset(int prunum) {
prussdrv_pru_disable(prunum);
prussdrv_pru_reset(prunum);
}
//---------------------------------------------------------------------------
void spi_reset_cmd(void) {
int i;
int N;
volatile uint32_t tmp;
printf("Sending reset command to PRU.....\n");
pru_write_word(0, SPI_RESET);
// Now wait until write is complete.
N = 10000000;
for (i=0; i<N; i++) {
tmp = pru_read_word(0x00);
// printf("...tmp = %d\n", tmp);
if (!tmp) {
break;
}
}
// printf("In spi_reset_cmd, at end of waiting, i = %d, tmp = %d\n", i, tmp);
}
//--------------------------------------------------------------
uint32_t pru_read_word(uint32_t offset) {
// Must make these volatile so compiler evaluates them
// every time this fcn is called.
uint32_t *mem_ptr;
uint32_t *mem_ptr_32;
const uint32_t ram_offset = RAMOFFSET;
uint32_t retval;
mem_ptr = pru0_dataram;
mem_ptr_32 = mem_ptr + ram_offset + offset;
msync(mem_ptr_32, 1, MS_SYNC);
retval = *mem_ptr_32;
//printf("--> In pru_read_word, just read 0x%08x\n", retval);
return retval;
}
//--------------------------------------------------------------
void pru_write_word(uint32_t offset, uint32_t value) {
uint32_t *mem_ptr;
uint32_t *mem_ptr_32;
const uint32_t ram_offset = RAMOFFSET;
mem_ptr = pru0_dataram;
mem_ptr_32 = mem_ptr + ram_offset + offset;
*mem_ptr_32 = value;
msync(mem_ptr_32, 1, MS_SYNC);
//printf("--> In pru_write_word, just wrote 0x%08x\n", value);
}
//--------------------------------------------------------------
uint32_t pru_test_ram(uint32_t offset, uint32_t value) {
// Use this fcn to verify that the PRU RAM is configured correctly.
uint32_t *mem_ptr;
uint32_t *mem_ptr_32;
const uint32_t ram_offset = RAMOFFSET;
uint32_t retval;
printf("----> Entered pru_test_ram, value to write = %d\n", value);
// Write input to PRU RAM
pru_write_word(offset, value);
// Read value back out PRU RAM
retval = pru_read_word(offset);
printf(" In pru_test_ram, value read back = %d\n", retval);
if (value == retval) {
printf(" ram test passed!\n");
} else {
printf(" ram test faialed!\n");
}
printf("<---- Leaving pru_test_ram\n");
return retval;
}
//--------------------------------------------------------------
uint32_t pru_test_communication(void) {
int i;
int N;
volatile uint32_t tmp;
// Now give it flag so it can go and do its thing
printf("------> Entered pru_test_communication....\n");
printf(" send SPI_TEST to PRU to test communication link...\n");
pru_write_word(0, SPI_TEST);
// Now wait until write is complete.
N = 1000000;
for (i=0; i<N; i++) {
tmp = pru_read_word(0x00);
// printf("...tmp = %d\n", tmp);
if (!tmp) {
break;
}
}
printf(" In pru_test_communication, at end of waiting, i = %d, tmp = %d\n", i, tmp);
if (i == N) {
printf(" Communications test failed!\n");
} else {
printf(" Communications test passed!\n");
}
printf("<------ Leaving pru_test_communication.\n");
return i;
}
//===============================================================
// These are high-level fcns which perform a command or a conversion read.
//--------------------------------------------------------------
uint32_t spi_write_cmd(uint32_t *data, int word_cnt) {
// Pass a word count and a pointer to the data, send command to
// PRU0 (SPI master) to pass on to A/D.
// Max word count is 3 -- corresponding to max write len to AD7172.
uint32_t retval = 0;
/* Command message structure is:
uint32_t flag -- specifying what command to do
uint32_t word_count
uint32_t data[4]
*/
uint32_t i;
volatile uint32_t tmp;
uint32_t mem_ptr = 0x00;
uint32_t N;
// printf("--> In spi_write_cmd, writing %d words\n", word_cnt);
// First set up PRU0 memory with data I want to transmit
pru_write_word(mem_ptr++, 0xff);
pru_write_word(mem_ptr++, word_cnt);
for (i = 0; i < word_cnt; i++) {
pru_write_word(mem_ptr++, data[i]);
// printf(" Tx byte %d = 0x%08x\n", i, data[i]);
}
// Now give it flag so it can go and do its thing
// printf("Send SPI_WRITE to PRU to do execute SPI write...\n");
pru_write_word(0, SPI_WRITE);
// Now wait until write is complete.
N = 10000000;
for (i=0; i<N; i++) {
tmp = pru_read_word(0x00);
if (!tmp) {
break;
}
}
if (i == N) {
printf("In spi_write_cmd, timed out waiting for end of transaction!\n");
pru_reset(PRU0);
prussdrv_exit();
exit(-1);
}
// printf("In spi_write_cmd, at end of waiting, i = %d, tmp = %d\n", i, tmp);
return retval;
}
//--------------------------------------------------------------
uint8_t spi_writeread_single(uint32_t *txdata, int txcnt, uint32_t *rxdata, int rxcnt) {
// Pass a byte count and a pointer to the data, send command to
// PRU0 (SPI master) to pass on to A/D, then read response.
// This fcn takes care of translating the input data (bytes) into the data sent
// to the PRU (uint32_t).
// This version first asserts CS down, then waits until MISO goes up then down
// before sending the "get data" command.
/* Command message structure is:
uint32_t flag -- specifying what command to do
uint32_t tx_byte_count
uint32_t tx_data[<tx_byte_count>]
uint32_t rx_byte_count
uint32_t rx_data[<rx_byte_count>]
*/
uint32_t i;
volatile int tmp;
uint32_t memptr = 0x00;
uint32_t rxptr;
uint32_t N;
// printf("--> In spi_writeread_single, writing %d words\n", txcnt);
// Set up transmitted data
pru_write_word(memptr++, 0xff); // Put PRU in "Wait for command" mode
pru_write_word(memptr++, txcnt); // Number of tx bytes to send.
for (i = 0; i < txcnt; i++) {
pru_write_word(memptr++, (uint32_t) txdata[i]);
// printf(" Tx byte %d = 0x%08x\n", i, txdata[i]);
}
// Set up receive buffer
pru_write_word(memptr++, rxcnt);
rxptr = memptr; // This points to begin of rx data.
for (i = 0; i < rxcnt; i++) {
pru_write_word(memptr++, (uint32_t) 0x00);
}
// Now send the instruction flag.
pru_write_word(0, SPI_WRITEREAD_SINGLE);
// Wait for transaction to complete.
N = 10000000;
// N = 100;
for (i=0; i<N; i++) {
tmp = pru_read_word(0x00);
// printf("In spi_writeread_single, tmp = 0x%08x\n", tmp);
if (!(tmp)) {
break;
}
}
if (i == N) {
printf("In spi_writeread_single, timed out waiting for end of transaction!\n");
pru_reset(PRU0);
prussdrv_exit();
exit(-1);
}
// printf("After waiting for SPI_WRITEREAD_SINGLE, i = %d, tmp = 0x%02x\n", i, tmp);
// At end of transaction, the received data should be placed into
// rx_data.
rxdata[0] = pru_read_word(rxptr);
// printf(" Rx data %d = 0x%08x\n", i, rxdata[0]);
// May want to return number of received words here
return rxcnt;
}
//---------------------------------------------------------------------------
uint8_t spi_writeread_continuous(uint32_t *txdata, int txcnt, uint32_t *rxdata, int rxcnt, int ncnv) {
// The only difference between this fcn and writeread_single is
// that this fcn invokes the SPI_WRITEREAD_CONTINUOUS method
// on the PRU.
/* Command message structure is:
uint32_t flag -- specifying what command to do
uint32_t tx_byte_count
uint32_t tx_data[<tx_byte_count>]
uint32_t rx_byte_count
uint32_t rx_data[<rx_byte_count>]
*/
uint32_t i;
volatile int tmp;
uint32_t memptr = 0x00;
uint32_t rxptr;
uint32_t N;
// printf("--> In spi_writeread_continuous, writing %d bytes\n", txcnt);
// Set up transmitted data
pru_write_word(memptr++, 0xff); // Put PRU in "Wait for command" mode
pru_write_word(memptr++, txcnt); // Number of tx bytes to send.
for (i = 0; i < txcnt; i++) {
pru_write_word(memptr++, txdata[i]);
// printf(" Tx word %d = 0x%08x\n", i, txdata[i]);
}
// Set up receive buffer
pru_write_word(memptr++, rxcnt); // Number of bytes in one conversion value
pru_write_word(memptr++, ncnv); // Total number of conversions requested
rxptr = memptr; // This points to begin of rx data.
for (i = 0; i < ncnv; i++) {
pru_write_word(memptr++, 0x00);
}
// Now send the instruction flag.
pru_write_word(0, SPI_WRITEREAD_CONTINUOUS);
// Wait for transaction to complete.
N = 10000000;
for (i=0; i<N; i++) {
tmp = pru_read_word(0x00);
// printf("In spi_writeread_continuous, tmp = 0x%08x\n", tmp);
if (!(tmp)) {
break;
}
}
if (i == N) {
printf("In spi_writeread_continuous, timed out waiting for end of transaction!\n");
pru_reset(PRU0);
prussdrv_exit();
exit(-1);
}
// printf("After waiting for SPI_WRITEREAD_CONTINUOUS, i = %d, tmp = 0x%02x\n", i, tmp);
// At end of transaction, the received data should be placed into
// rx_data.
for (i = 0; i < ncnv; i++) {
rxdata[i] = pru_read_word(rxptr+i);
// printf(" Rx word %d = 0x%08x\n", i, rxdata[i]);
}
// May want to return number of received words here
return ncnv;
}