|
| 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 | + |
0 commit comments