| 
 | 1 | + | 
 | 2 | + | 
 | 3 | +/* The ESP32 has four SPi buses, however as of right now only two of  | 
 | 4 | + * them are available to use, HSPI and VSPI. Simply using the SPI API   | 
 | 5 | + * as illustrated in Arduino examples will use HSPI, leaving VSPI unused.  | 
 | 6 | + *   | 
 | 7 | + * However if we simply intialise two instance of the SPI class for both  | 
 | 8 | + * of these buses both can be used. However when just using these the Arduino  | 
 | 9 | + * way only will actually be outputting at a time.  | 
 | 10 | + *   | 
 | 11 | + * Logic analyser capture is in the same folder as this example as  | 
 | 12 | + * "multiple_bus_output.png"  | 
 | 13 | + *   | 
 | 14 | + * created 30/04/2018 by Alistair Symonds  | 
 | 15 | + */  | 
 | 16 | +#include <SPI.h>  | 
 | 17 | + | 
 | 18 | +static const int spiClk = 1000000; // 1 MHz  | 
 | 19 | + | 
 | 20 | +//uninitalised pointers to SPI objects  | 
 | 21 | +SPIClass * vspi = NULL;  | 
 | 22 | +SPIClass * hspi = NULL;  | 
 | 23 | + | 
 | 24 | +void setup() {  | 
 | 25 | + //initialise two instances of the SPIClass attached to VSPI and HSPI respectively  | 
 | 26 | + vspi = new SPIClass(VSPI);  | 
 | 27 | + hspi = new SPIClass(HSPI);  | 
 | 28 | +   | 
 | 29 | + //clock miso mosi ss  | 
 | 30 | + | 
 | 31 | + //initialise vspi with default pins  | 
 | 32 | + //SCLK = 18, MISO = 19, MOSI = 23, SS = 5  | 
 | 33 | + vspi->begin();  | 
 | 34 | + //alternatively route through GPIO pins of your choice  | 
 | 35 | + //hspi->begin(5,6,7,8); //SCLK, MISO, MOSI, SS  | 
 | 36 | +   | 
 | 37 | + //initialise hspi with default pins  | 
 | 38 | + //SCLK = 14, MISO = 12, MOSI = 13, SS = 15  | 
 | 39 | + hspi->begin();   | 
 | 40 | + //alternatively route through GPIO pins  | 
 | 41 | + //hspi->begin(1,2,3,4); //SCLK, MISO, MOSI, SS  | 
 | 42 | + | 
 | 43 | + //set up slave select pins as outputs as the Arduino API  | 
 | 44 | + //doesn't handle automatically pulling SS low  | 
 | 45 | + pinMode(5, OUTPUT); //VSPI SS  | 
 | 46 | + pinMode(15, OUTPUT); //HSPI SS  | 
 | 47 | + | 
 | 48 | +}  | 
 | 49 | + | 
 | 50 | +// the loop function runs over and over again until power down or reset  | 
 | 51 | +void loop() {  | 
 | 52 | + //use the SPI buses  | 
 | 53 | + vspiCommand();  | 
 | 54 | + hspiCommand();  | 
 | 55 | + delay(100);  | 
 | 56 | +}  | 
 | 57 | + | 
 | 58 | +void vspiCommand() {  | 
 | 59 | + byte data = 0b01010101; // junk data to illustrate usage  | 
 | 60 | + | 
 | 61 | + //use it as you would the regular arduino SPI API  | 
 | 62 | + vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));  | 
 | 63 | + digitalWrite(5, LOW); //pull SS slow to prep other end for transfer  | 
 | 64 | + vspi->transfer(data);   | 
 | 65 | + digitalWrite(5, HIGH); //pull ss high to signify end of data transfer  | 
 | 66 | + vspi->endTransaction();  | 
 | 67 | +}  | 
 | 68 | + | 
 | 69 | +void hspiCommand() {  | 
 | 70 | + byte stuff = 0b11001100;  | 
 | 71 | +   | 
 | 72 | + hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));  | 
 | 73 | + digitalWrite(15, LOW);  | 
 | 74 | + hspi->transfer(stuff);  | 
 | 75 | + digitalWrite(15, HIGH);  | 
 | 76 | + hspi->endTransaction();  | 
 | 77 | +}  | 
0 commit comments