-
Notifications
You must be signed in to change notification settings - Fork 18
/
Readme.txt
516 lines (293 loc) · 13.4 KB
/
Readme.txt
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
This library was written specifically for the TEENSy 3.0 SPI. It will not work on Arduino or any other system.
This library supports using the alternate SPI pins, the gray pins on the pinout diagram.
***
It should be noted that I experienced some problems when using SLAVE mode, with 8 bit frames,
with a large array length, at the fastest clock rate. I was able to go to the next clock
divider (SPI_CLOCK_DIV4 = 12.0 MHz) and this solved my problem. I can also use a smaller
array length with no issues. I think that at very high speed, with short frame size, the
master SPI can over-run my slave ISR scan time.
***
The SLAVE "return data" is loaded every time the ISR is called. So on the first byte received,
there will be no data loaded into the "return data" register. So expect no return data (zero's)
on the very first frame transferred.
***
Many slave devices require that the CS pin stay active until several bytes have been transfered.
For these, it may be useful to specify your own CS pin and do a digitalWrite(CSpin, state) outside
of the tx function.
Example:
digitalWrite(CS18, LOW);
t3SPI.tx8(data1, CTARn, CS);
t3SPI.tx8(data2, CTARn, CS);
digitalWrite(CS18, HIGH);
******* F U N C T I O N D E F I N I T I O N S *******
********************************************
******* FUNCTIONS FOR MASTER MODE *******
begin_MASTER();
******* This funtion initializes the SPI(0) to its most basic configuration.
SPI: Master Mode
CTAR: CTAR0
Clock Divider: 8 = 5.3 Mhz
Frame Size: 8 bits
SPI Mode: 0
Bit Order: LSB First
CS State: Active Low
Clock: Pin 13 (output)
MOSI: Pin 11 (output)
MISO: Pin 12 (intput)
CS0: Pin 10 (output)
begin_MASTER(sck, mosi, miso, cs, activeState);
******* This funtion initializes the SPI(0) with user-defined pins.
You can initialize Alternate Pins here.
sck OPTIONS: SCK = Pin 13
ALT_SCK = Pin 14
mosi OPTIONS: MOSI = Pin 11
ALT_MOSI = Pin 7
miso OPTIONS: MISO = Pin 12
ALT_MISO = Pin 8
cs OPTIONS: CS0 = Pin 10
CS1 = Pin 9
CS2 = Pin 20
CS3 = Pin 21
CS4 = Pin 15
ALT_CS0 = Alternate CS0 on Pin 2
ALT_CS1 = Alternate CS1 on Pin 6
ALT_CS2 = Alternate CS2 on Pin 23
ALT_CS3 = Alternate CS3 on Pin 22
activeState OPTIONS: ActiveLOW = CS pin normally HIGH, goes LOW durring transfer
ActiveHIGH = CS pin normally LOW, goes HIGH durring transfer
setCTAR(CTARn, size, dataMode, bo, cdiv);
******* This funtion configures the CTARn
The CTAR's specify how data will be sent out.
There are two CTAR registers, CTAT0 and CTAR1.
This is useful when you are using one Master to communicate to multiple slave devices.
CTARn OPTIONS: CTAR0 = configures CTAR0
CTAR1 = configures CTAR1
size OPTIONS: 8 = sets a frame size of 8 bits (per transfer)
16 = sets a frame size of 16 bits (per transfer)
dataMode OPTIONS: 0 - 3 = industry standard SPI clock modes, see wikipedia SPI Mode
bo OPTIONS: LSB_FIRST = Least significant bit transfered first
MSB_FIRST = Most significant bit transfered first.
cdiv OPTIONS: #define SPI_CLOCK_DIV2 0b0000 //24.0 MHz
SPI_CLOCK_DIV4 = 12.0 MHz
SPI_CLOCK_DIV6 = 08.0 MHz
SPI_CLOCK_DIV8 = 05.3 MHz
SPI_CLOCK_DIV16 = 03.0 MHz
SPI_CLOCK_DIV32 = 01.5 MHz
SPI_CLOCK_DIV64 = 750 KHz
SPI_CLOCK_DIV128= 375 KHz
There are more modes available, but not includeded in this library.
enableCS(cs, activeState);
******* This funtion enables CS pins.
This is useful if you want to use more CS pins to control multiple SPI devices.
You can initialize Alternate Pins here.
cs OPTIONS: CS0 = Pin 10
CS1 = Pin 9
CS2 = Pin 20
CS3 = Pin 21
CS4 = Pin 15
ALT_CS0 = Alternate CS0 on Pin 2
ALT_CS1 = Alternate CS1 on Pin 6
ALT_CS2 = Alternate CS2 on Pin 23
ALT_CS3 = Alternate CS3 on Pin 22
activeState OPTIONS: ActiveLOW = CS pin normally HIGH, goes LOW durring transfer
ActiveHIGH = CS pin normally LOW, goes HIGH durring transfer
tx8(data, length, CTARn, PCS);
******* This funtion sends a packet of 8 bit data
dataOUT OPTIONS: This is where you put the name of your (8 bit) outgoing data array
length OPTIONS: Put the length of the outgoing data array here
CTARn OPTIONS: CTAR0 :Use the attributes that were set in CTAR0
CTAR1 :Use the attributes that were set in CTAR1
PCS OPTIONS: Any CS pin what has been enabled. (example: CS0, ALT_CS2)
tx16(data, length, CTARn, PCS);
******* This funtion sends a packet of 16 bit data
dataOUT OPTIONS: This is where you put the name of your (16 bit) outgoing data array
length OPTIONS: Put the length of the outgoing data array here
CTARn OPTIONS: CTAR0 :Use the attributes that were set in CTAR0
CTAR1 :Use the attributes that were set in CTAR1
PCS OPTIONS: Any CS pin what has been enabled. (example: CS0, ALT_CS2)
txrx8(dataOUT, dataIN, length, CTARn, PCS);
******* This funtion sends a packet of 8 bit data & receives a packet of 8 bit data from the slave device
dataOUT OPTIONS: This is where you put the name of your (8 bit) outgoing data array
dataIN OPTIONS: This is where you put the name of your (8 bit) incoming data array
length OPTIONS: Put the length of the outgoing data array here
CTARn OPTIONS: CTAR0 :Use the attributes that were set in CTAR0
CTAR1 :Use the attributes that were set in CTAR1
PCS OPTIONS: Any CS pin what has been enabled. (example: CS0, ALT_CS2)
txrx16(dataOUT, dataIN, length, CTARn, PCS);
******* This funtion sends a packet of 16 bit data & receives a packet of 16 bit data from the slave device
dataOUT OPTIONS: This is where you put the name of your (16 bit) outgoing data array
dataIN OPTIONS: This is where you put the name of your (16 bit) incoming data array
length OPTIONS: Put the length of the outgoing data array here
CTARn OPTIONS: CTAR0 :Use the attributes that were set in CTAR0
CTAR1 :Use the attributes that were set in CTAR1
PCS OPTIONS: Any CS pin what has been enabled. (example: CS0, ALT_CS2)
********************************************
******* FUNCTIONS FOR SLAVE MODE ********
begin_SLAVE();
******* This funtion initializes the SPI(0) to its most basic configuration.
SPI: SLAVE Mode
CTAR: CTAR0_SLAVE
Frame Size: 8 bits
SPI Mode: 0
Clock: Pin 13 (input)
MOSI: Pin 11 (output)
MISO: Pin 12 (input)
CS0: Pin 10 (input)
begin_SLAVE(sck, mosi, miso, cs);
******* This funtion initializes the SPI(0) with user-defined pins.
You can initialize Alternate Pins here.
CS is always CS0 (or ALT_CS0) & ACTIVE_LOW in slave mode.
sck OPTIONS: SCK = Pin 13
ALT_SCK = Pin 14
mosi OPTIONS: MOSI = Pin 11
ALT_MOSI = Pin 7
miso OPTIONS: MISO = Pin 12
ALT_MISO = Pin 8
cs OPTIONS: CS0 = Pin 10
ALT_CS0 = Alternate CS0 on Pin 2
setCTAR_SLAVE(size, dataMode);
******* This funtion configures the CTAR0_SLAVE.
The CTAR's specify how data will be received.
SPI0_CTAR0_SLAVE is the CTAR used in SLAVE Mode.
Data is always shifted out LSB_FIRST in SLAVE Mode.
The Frame Size MUST be the same as defined on the MASTER device.
The dataMode should be the same as defined on the MASTER device.
size OPTIONS: 8 = sets a frame size of 8 bits (per transfer)
16 = sets a frame size of 16 bits (per transfer)
dataMode OPTIONS: 0 - 3 = industry standard SPI clock modes, see wikipedia SPI Mode
******* NOTE *******
The SPI SLAVE uses an Interrupt Service Routine to notify the device that a frame of data has been received.
A rx function must be used in ISR to tell the device how to handle it received data.
It looks like this (example):
void spi0_isr(void){
rx8(outgoingData, dataLength);}
The following 4 functions can be put in the ISR to handle incoming data. Do not use more than rx function in the ISR.
rx8(dataIN, length);
******* This funtion receives a packet of 8 bit data
dataIN OPTIONS: This is where you put the name of your (8 bit) incoming data array
length OPTIONS: Put the length of the incoming data array here
rx16(dataIN, length);
******* This funtion receives a packet of 16 bit data
dataIN OPTIONS: This is where you put the name of your (16 bit) incoming data array
length OPTIONS: Put the length of the incoming data array here
rxtx8(dataIN, dataOUT, length);
******* This funtion receives a packet of 8 bit data & returns a packet of 8 bit data to the master device
dataIN OPTIONS: This is where you put the name of your (8 bit) incoming data array
dataOUT OPTIONS: This is where you put the name of your (8 bit) outgoing data array
length OPTIONS: Put the length of the incoming data array here
rxtx16(dataIN, dataOUT, length);
******* This funtion receives a packet of 16 bit data & returns a packet of 16 bit data to the master device
dataIN OPTIONS: This is where you put the name of your (16 bit) incoming data array
dataOUT OPTIONS: This is where you put the name of your (16 bit) outgoing data array
length OPTIONS: Put the length of the incoming data array here
********************************************
************* GLOBAL FUNCTIONS *************
start();
******* This enables SPI functions.
stop();
******* This halts SPI functions.
end();
******* This disables SPI functions.
printStatistics(length);
******* This function prints certain SPI statistics to the Serial monitor.
You must set the following variables to get acurate data:
T3SPI.timeStamp1=micros();
T3SPI.timeStamp2=micros();
length OPTIONS: Put the length of the outgoing or incoming data array here
********************************************
************ PRIVATE FUNCTIONS *************
******* These functions cannot be used in the sketch but are used within the class.
setMCR(mode);
******* This funtion initializes the SPI(0) to basic Master or Slave Mode.
mode OPTIONS: MASTER = Master SPI Mode
SLAVE = Slave SPI Mode
setFrameSize(CTARn, size);
******* This funtion sets the frame size.
CTARn OPTIONS: CTAR0 = configures CTAR0
CTAR1 = configures CTAR1
CTAR_SLAVE = configures CTAR0_SLAVE
size OPTIONS: 8 = sets a frame size of 8 bits (per transfer)
16 = sets a frame size of 16 bits (per transfer)
setMode(CTARn, dataMode);
******* This funtion sets the spi data mode.
CTARn OPTIONS: CTAR0 = configures CTAR0
CTAR1 = configures CTAR1
dataMode OPTIONS: 0 - 3 = industry standard SPI clock modes, see wikipedia SPI Mode
setBitOrder(CTARn, bo);
******* This funtion sets the outgoing bit order.
CTARn OPTIONS: CTAR0 = configures CTAR0
CTAR1 = configures CTAR1
bo OPTIONS: LSB_FIRST = Least significant bit transfered first
MSB_FIRST = Most significant bit transfered first.
setClockDivider(CTARn, cdiv);
******* This funtion sets the clock divider in MASTER mode.
CTARn OPTIONS: CTAR0 = configures CTAR0
CTAR1 = configures CTAR1
cdiv OPTIONS: #define SPI_CLOCK_DIV2 0b0000 //24.0 MHz
SPI_CLOCK_DIV4 = 12.0 MHz
SPI_CLOCK_DIV6 = 08.0 MHz
SPI_CLOCK_DIV8 = 05.3 MHz
SPI_CLOCK_DIV16 = 03.0 MHz
SPI_CLOCK_DIV32 = 01.5 MHz
SPI_CLOCK_DIV64 = 750 KHz
SPI_CLOCK_DIV128= 375 KHz
There are more modes available, but not includeded in this library.
enablePins(sck, mosi, miso, cs, activeState);
******* This funtion enables the SPI pins
You can initialize Alternate Pins here.
sck OPTIONS: SCK = Pin 13
ALT_SCK = Pin 14
mosi OPTIONS: MOSI = Pin 11
ALT_MOSI = Pin 7
miso OPTIONS: MISO = Pin 12
ALT_MISO = Pin 8
cs OPTIONS: CS0 = Pin 10
CS1 = Pin 9
CS2 = Pin 20
CS3 = Pin 21
CS4 = Pin 15
ALT_CS0 = Alternate CS0 on Pin 2
ALT_CS1 = Alternate CS1 on Pin 6
ALT_CS2 = Alternate CS2 on Pin 23
ALT_CS3 = Alternate CS3 on Pin 22
activeState OPTIONS: ActiveLOW = CS pin normally HIGH, goes LOW durring transfer
ActiveLOW = CS pin normally LOW, goes HIGH durring transfer
enablePins_SLAVE(sck, mosi, miso, cs);
******* This funtion enables the SPI pins for SLAVE mode.
You can initialize Alternate Pins here.
sck OPTIONS: SCK = Pin 13
ALT_SCK = Pin 14
mosi OPTIONS: MOSI = Pin 11
ALT_MOSI = Pin 7
miso OPTIONS: MISO = Pin 12
ALT_MISO = Pin 8
cs OPTIONS: CS0 = Pin 10
ALT_CS0 = Alternate CS0 on Pin 2
setCS_ActiveLOW(pin);
******* This funtion sets CS pins as ACTIVE_LOW.
CS pins initialize to ACTIVE_HIGH
pin OPTIONS: CS0_ActiveLow = Sets the CS as ACTIVE_LOW
CS1_ActiveLow = Sets the CS as ACTIVE_LOW
CS2_ActiveLow = Sets the CS as ACTIVE_LOW
CS3_ActiveLow = Sets the CS as ACTIVE_LOW
CS4_ActiveLow = Sets the CS as ACTIVE_LOW
********************************************
************** Defined Macros **************
As long as the T3SPI is #included, these three macros can be used outside of the T3SPI class.
Because they are defined, you don't need to use the class pointer to use them.
*Use SPI_WRITE_16, not use t3SPI.SPI_WRITE_16
SPI_WRITE_16(data, CTARn, CS);
******* This macro sends a 16 bit byte.
data OPTIONS: put your 16 bit variable here.
CTARn OPTIONS: CTAR0 :Use the attributes that were set in CTAR0
CTAR1 :Use the attributes that were set in CTAR1
PCS OPTIONS: Any CS pin what has been enabled. (example: CS0, ALT_CS2)
SPI_WRITE_8(data, CTARn, CS);
******* This macro sends a 8 bit byte.
data OPTIONS: put your 8 bit variable here.
CTARn OPTIONS: CTAR0 :Use the attributes that were set in CTAR0
CTAR1 :Use the attributes that were set in CTAR1
PCS OPTIONS: Any CS pin what has been enabled. (example: CS0, ALT_CS2)
SPI_WAIT( );
******* This macro waits for the current transfer to finish sending. It is useful for many
standard speed slave devices.