Skip to content

Commit 4179f52

Browse files
authored
Merge pull request chipKIT32#350 from EmbeddedMan/BPS_Dev
Fix for issue chipKIT32#349 : Added two example sketches using the DSPI library.
2 parents ed1436d + 8aac38b commit 4179f52

File tree

4 files changed

+257
-0
lines changed

4 files changed

+257
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* This is a very simple test of the DSPI library, using the three available SPI ports
3+
* on a Fubarino SD chipKIT board. To run this test, simply connect the MOSI and MISO
4+
* pins of each of the three SPI ports to each other. This test sends one set of 3
5+
* bytes out SPI0, back in SPI0, then out SPI1, back in SPI1, and out SPI2, back in SPI2.
6+
* It then prints out all of the received bytes to the USB serial port. If everything is
7+
* working you should get the following printed to the serial monitor:
8+
* 55:AA:A5 55:AA:A5 55:AA:A5
9+
* See https://github.com/chipKIT32/chipKIT-core/issues/349 for more information includeing
10+
* a logic analyzer trace of what this looks like as it runs.
11+
*/
12+
13+
#include <DSPI.h>
14+
15+
/* Note these pin numbers are for Fubarino SD */
16+
DSPI0 spi0; /* SCK = 24, MISO = 25, MOSI = 26, CS = 27 */
17+
DSPI1 spi1; /* SCK = 7, MISO = 8, MOSI = 9, CS = 1 */
18+
DSPI2 spi2; /* SCK = 43, MISO = 28, MOSI = 29, CS = 37 */
19+
20+
uint8_t sendBuffer[3] = {0x55, 0xAA, 0xA5};
21+
uint8_t receiveBuffer1[3];
22+
uint8_t receiveBuffer2[3];
23+
uint8_t receiveBuffer3[3];
24+
25+
void setup() {
26+
// Make all CS lines outputs
27+
pinMode(27, OUTPUT);
28+
pinMode(1, OUTPUT);
29+
pinMode(37, OUTPUT);
30+
// Use different speeds so we can tell which port is which on the logic analzyer
31+
// Simple begin, no arguments. Defaults to 1Mhz clock and DSPI_MODE0, and default SS pin for this port
32+
spi0.begin();
33+
spi0.setSpeed(5000000);
34+
35+
// Single argument to begin() is the pin you want to use as slave select (if different from default)
36+
spi1.begin(1);
37+
spi1.setSpeed(10000000);
38+
// What if we don't want MODE0? We can set any of the four modes using setMode()
39+
spi1.setMode(DSPI_MODE1);
40+
41+
spi2.begin();
42+
spi2.setSpeed(20000000);
43+
// We can also set the pin select pin using the setPinSelect() call
44+
spi2.setPinSelect(37);
45+
46+
Serial.begin(9600);
47+
while(!Serial); // Wait for PC to connect
48+
}
49+
50+
void loop() {
51+
spi0.setSelect(LOW);
52+
// Let's do this one byte at a time for spi0
53+
receiveBuffer1[0] = spi0.transfer(sendBuffer[0]);
54+
receiveBuffer1[1] = spi0.transfer(sendBuffer[1]);
55+
receiveBuffer1[2] = spi0.transfer(sendBuffer[2]);
56+
spi0.setSelect(HIGH);
57+
58+
spi1.setSelect(LOW);
59+
spi1.transfer(3, receiveBuffer1, receiveBuffer2);
60+
spi1.setSelect(HIGH);
61+
62+
spi2.setSelect(LOW);
63+
spi2.transfer(3, receiveBuffer2, receiveBuffer3);
64+
spi2.setSelect(HIGH);
65+
66+
Serial.print(receiveBuffer1[0], HEX);
67+
Serial.print(":");
68+
Serial.print(receiveBuffer1[1], HEX);
69+
Serial.print(":");
70+
Serial.print(receiveBuffer1[2], HEX);
71+
Serial.print(" ");
72+
73+
Serial.print(receiveBuffer2[0], HEX);
74+
Serial.print(":");
75+
Serial.print(receiveBuffer2[1], HEX);
76+
Serial.print(":");
77+
Serial.print(receiveBuffer2[2], HEX);
78+
Serial.print(" ");
79+
80+
Serial.print(receiveBuffer3[0], HEX);
81+
Serial.print(":");
82+
Serial.print(receiveBuffer3[1], HEX);
83+
Serial.print(":");
84+
Serial.print(receiveBuffer3[2], HEX);
85+
Serial.println("");
86+
}
87+
88+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
DSPI_Demo Readme
2+
3+
This demo sketch simply shows how to use multiple SPI ports
4+
on the same chipKIT board with the DSPI library.
5+
6+
Setup:
7+
8+
This sketch assumes that your chipKIT board has at least 3 SPI ports,
9+
and that each SPI port's MOSI is connected to its MISO. This 'loopback'
10+
configuration simply sends data out and receives it back in again.
11+
12+
Each of the three SPI ports is set to a different clock speed so that
13+
it is clear which is which on the logic analyzer trace.
14+
15+
This sketch is set up for the Fubarino SD board, but it can easily
16+
be adopted to any chipKIT board by changing the number of DSPI objects
17+
and the chip select pins (if necessary - it's fine to leave them
18+
at their default values too).
19+
20+
Flow:
21+
22+
Data is sent out DSPI0, then received back in DSPI0. That received
23+
data is then sent out DSPI1, received back in DSPI1, then that received
24+
data is sent out DSPI2, received back in DSPI2, then printed out
25+
to the USB serial port to the PC. If there are any errors, you will not
26+
see the proper printout in the serial monitor.
27+
28+
Expected Output:
29+
30+
You should see "55:AA:A5 55:AA:A5 55:AA:A5".
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* This is a very simple test of the DSPI library with interrupts, using the three available SPI ports
3+
* on a Fubarino SD chipKIT board. To run this test, simply connect the MOSI and MISO
4+
* pins of each of the three SPI ports to each other. This example code is based around the ability
5+
* of the DSPI library to use interrupts to peform the SPI transfers in the backgrounds. Because of
6+
* this, and because we want to show overlapping SPI transactions, we have to use six separate buffers,
7+
* three transmit and three receive. These buffers must be statically allocated (or through malloc())
8+
* so that the interrupts can have access to them during the entire transaction.
9+
* It then prints out all of the received bytes to the USB serial port. If everything is
10+
* working you should get the following printed to the serial monitor:
11+
* 11:22:33 44:55:66 77:88:99
12+
* See https://github.com/chipKIT32/chipKIT-core/issues/349 for more information includeing
13+
* a logic analyzer trace of what this looks like as it runs.
14+
*
15+
* The whole point of using interrupt based SPI transfers is so that you can do other things
16+
* with your code while the SPI transaction happens in the background.
17+
*/
18+
19+
#include <DSPI.h>
20+
21+
/* Note these pin numbers are for Fubarino SD */
22+
DSPI0 spi0; /* SCK = 24, MISO = 25, MOSI = 26, CS = 27 */
23+
DSPI1 spi1; /* SCK = 7, MISO = 8, MOSI = 9, CS = 1 */
24+
DSPI2 spi2; /* SCK = 43, MISO = 28, MOSI = 29, CS = 37 */
25+
26+
uint8_t sendBuffer1[3] = {0x11, 0x22, 0x33};
27+
uint8_t sendBuffer2[3] = {0x44, 0x55, 0x66};
28+
uint8_t sendBuffer3[3] = {0x77, 0x88, 0x99};
29+
uint8_t receiveBuffer1[3];
30+
uint8_t receiveBuffer2[3];
31+
uint8_t receiveBuffer3[3];
32+
33+
void setup() {
34+
// Make all CS lines outputs
35+
pinMode(27, OUTPUT);
36+
pinMode(1, OUTPUT);
37+
pinMode(37, OUTPUT);
38+
// Use different speeds so we can tell which port is which on the logic analzyer
39+
// Simple begin, no arguments. Defaults to 1Mhz clock and DSPI_MODE0, and default SS pin for this port
40+
spi0.begin();
41+
spi0.setSpeed(5000000);
42+
spi0.enableInterruptTransfer();
43+
44+
// Single argument to begin() is the pin you want to use as slave select (if different from default)
45+
spi1.begin(1);
46+
spi1.setSpeed(10000000);
47+
// What if we don't want MODE0? We can set any of the four modes using setMode()
48+
spi1.setMode(DSPI_MODE1);
49+
spi1.enableInterruptTransfer();
50+
51+
spi2.begin();
52+
spi2.setSpeed(20000000);
53+
// We can also set the pin select pin using the setPinSelect() call
54+
spi2.setPinSelect(37);
55+
spi2.enableInterruptTransfer();
56+
57+
Serial.begin(9600);
58+
while(!Serial); // Wait for PC to connect
59+
}
60+
61+
void loop() {
62+
spi0.setSelect(LOW);
63+
spi0.intTransfer(3, sendBuffer1, receiveBuffer1);
64+
// The intTransaction call will start our tx/rx over SPI in the background. The call
65+
// returns immediately.
66+
// Note that we can set the spi0 select pin high here, because the transaction isn't complete yet.
67+
68+
// Next we send spi1 and spi2's data out. All three transactions will end up happening at the same time.
69+
spi1.setSelect(LOW);
70+
spi1.intTransfer(3, sendBuffer2, receiveBuffer2);
71+
72+
spi2.setSelect(LOW);
73+
spi2.intTransfer(3, sendBuffer3, receiveBuffer3);
74+
75+
// Now, before we can deassert the slave select lines and trust that our receive buffers
76+
// are full, we have to wait until all DSPI objects have no more bytes waiting to go out.
77+
while(spi0.transCount() != 0 || spi1.transCount() != 0 || spi2.transCount() != 0)
78+
{
79+
;
80+
}
81+
spi0.setSelect(HIGH);
82+
spi1.setSelect(HIGH);
83+
spi2.setSelect(HIGH);
84+
85+
// Now we can print out all of the values we received
86+
Serial.print(receiveBuffer1[0], HEX);
87+
Serial.print(":");
88+
Serial.print(receiveBuffer1[1], HEX);
89+
Serial.print(":");
90+
Serial.print(receiveBuffer1[2], HEX);
91+
Serial.print(" ");
92+
93+
Serial.print(receiveBuffer2[0], HEX);
94+
Serial.print(":");
95+
Serial.print(receiveBuffer2[1], HEX);
96+
Serial.print(":");
97+
Serial.print(receiveBuffer2[2], HEX);
98+
Serial.print(" ");
99+
100+
Serial.print(receiveBuffer3[0], HEX);
101+
Serial.print(":");
102+
Serial.print(receiveBuffer3[1], HEX);
103+
Serial.print(":");
104+
Serial.print(receiveBuffer3[2], HEX);
105+
Serial.println("");
106+
}
107+
108+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
DSPI_Int_Demo Readme
2+
3+
This demo sketch simply shows how to use multiple SPI ports
4+
on the same chipKIT board with the DSPI library, but doing the
5+
transfers at the same time using interrupts.
6+
7+
Setup:
8+
9+
This sketch assumes that your chipKIT board has at least 3 SPI ports,
10+
and that each SPI port's MOSI is connected to its MISO. This 'loopback'
11+
configuration simply sends data out and receives it back in again.
12+
13+
Each of the three SPI ports is set to a different clock speed so that
14+
it is clear which is which on the logic analyzer trace.
15+
16+
This sketch is set up for the Fubarino SD board, but it can easily
17+
be adopted to any chipKIT board by changing the number of DSPI objects
18+
and the chip select pins (if necessary - it's fine to leave them
19+
at their default values too).
20+
21+
Flow:
22+
23+
Three data output buffers and three data input buffers are created.
24+
Different data is sent out each SPI port, at different speeds.
25+
Each transaction is started at about the same time using interrupts
26+
(intTransfer()). The code then waits until all three transfers have
27+
completed, and prints out the data received from each port.
28+
29+
Expected Output:
30+
31+
You should see "11:22:33 44:55:66 77:88:99".

0 commit comments

Comments
 (0)