2

I am using the following code in my esp32 using Arduino IDE for spi communication which is an example code -

 #include <SPI.h>
 
 static const int spiClk = 1000000; // 1 MHz
 
 //uninitalised pointers to SPI objects
 SPIClass * vspi = NULL;
 SPIClass * hspi = NULL;
 
 void setup() {
 //initialise two instances of the SPIClass attached to VSPI and HSPI respectively
 vspi = new SPIClass(VSPI);
 hspi = new SPIClass(HSPI);
 
 //clock miso mosi ss
 
 //initialise vspi with default pins
 //SCLK = 18, MISO = 19, MOSI = 23, SS = 5
 vspi->begin();
 //alternatively route through GPIO pins of your choice
 //hspi->begin(0, 2, 4, 33); //SCLK, MISO, MOSI, SS
 
 //initialise hspi with default pins
 //SCLK = 14, MISO = 12, MOSI = 13, SS = 15
 hspi->begin(); 
 //alternatively route through GPIO pins
 //hspi->begin(25, 26, 27, 32); //SCLK, MISO, MOSI, SS
 
 //set up slave select pins as outputs as the Arduino API
 //doesn't handle automatically pulling SS low
 pinMode(5, OUTPUT); //VSPI SS
 pinMode(15, OUTPUT); //HSPI SS
 
 }
 
 // the loop function runs over and over again until power down or reset
 void loop() {
 //use the SPI buses
 vspiCommand();
 hspiCommand();
 delay(100);
 }
 
 void vspiCommand() {
 byte data = 0b01010101; // junk data to illustrate usage
 
 //use it as you would the regular arduino SPI API
 vspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
 digitalWrite(5, LOW); //pull SS slow to prep other end for transfer
 vspi->transfer(data); 
 delay(500);
 digitalWrite(5, HIGH); //pull ss high to signify end of data transfer
 vspi->endTransaction();
 }
 
 void hspiCommand() {
 byte stuff = 0b11001100;
 
 hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
 digitalWrite(15, LOW);
 hspi->transfer(stuff);
 delay(500);
 digitalWrite(15, HIGH);
 hspi->endTransaction();
 }

I am able to see the toggling of the SS pin, but the SCLK pin does not show any pulses on the oscilloscope. It is a constant low. Should the pulses be given from an external source? If so, what is the purpose of this line - static const int spiClk = 1000000; // 1 MHz?

Rohit Gupta
6122 gold badges5 silver badges18 bronze badges
asked Apr 14, 2020 at 6:30
2
  • What device are you using? Some board vendors print the GPIOnum (which is different from the pin num) to the silk screen, some times a vendor uses a different layout than the reference design so you have to look up in the circuit diagram (if you get/have it) - I know the "search" for the SCLK pin and other mappings ;-) Commented Apr 14, 2020 at 10:19
  • I'm using esp32 wroom. Yes, I know that gpio 5 (pin 29)is chip shredder, gpio 18(pin 30) is clock pulses etc using vspi.i am getting those toggling pulses on gpio5 but am not able to see any transfer of data. I.e clock pulses on gpio18 or pin 30 Commented Apr 14, 2020 at 10:23

1 Answer 1

1

Try to set the pins explicitly to Output
EDITDifferent issues with the test programs indicate that acc. to espressif the pin has to explicitly set to high which is not done by the libs begin method (as of core 1.04)

 digitalWrite(30, HIGH); // turn on pullup resistors
 digitalWrite(xxx, HIGH); // turn on pullup resistors
 pinMode(30, OUTPUT); //VSPI SLCK
 pinMode(XX, OUTPUT); //HSPI SCLK

The ESP32 docs say:

gpio18 will output low level(because gpio18 is input mode by default). If you don’t want this behavior, you should configure gpio18 as output mode and set it to hight level before calling

As this is not the issue - the Wroom32 modules v1-3 have a problem with SPI when flashed in certain modes with 80Mhz So try flashing with 40Mhz. I check for my test program in the meantime.

So here is a working test routine:

/* SPI Test routine for Wroom32 
Connect the MISO with the MOSI pin*/
#include<SPI.h> //Library for SPI
#define SPI_SCK 18 // Serial Clock (Master Output)
#define SPI_MOSI 23 // Master Output,Slave Input
#define SPI_MISO 19 // Master Input, Slave Output
#define SPI_CS 5 // Chip Select, Slave Transmit Enable (active low, Master Output)
#define SS 5
char returnStr[40] = {'0円'};
char returnChr;
void setup () {
 Serial.begin(115200); // Set baud rate to 115200 for usart
 pinMode(SPI_CS, OUTPUT);
 pinMode(SPI_SCK, OUTPUT);
 pinMode(SPI_MOSI, OUTPUT);
 pinMode(SPI_MISO, INPUT);
 digitalWrite(SS, HIGH); // disable Slave Select
 SPI.begin (SPI_SCK, SPI_MISO, SPI_MOSI);
 SPI.setBitOrder(MSBFIRST);
 SPI.setDataMode(SPI_MODE0);
 SPI.setClockDivider(SPI_CLOCK_DIV8); // divide the clock by 8=2 MHz @ESP32
}
void loop () {
 char c;
 digitalWrite(SS, LOW); // enable Slave Select
 /* Send test string in a loop */
 for (const char *p = "SPI TEST! We are online over SPI\n\r" ; (c = *p); p++) {
 returnChr = SPI.transfer (c);
 Serial.print(c);
 Serial.print(returnChr); // MOSI MISO Link sends echo back
 }
 digitalWrite(SS, HIGH); // disable Slave Select
 delay(500);
}

To measure the signals you have to trigger to the falling edge of SS/CS and set the resolution to 2μs per grid field (round about). I tested on an AIThinker module (ESP32) and everything worked with that sketch.

answered Apr 14, 2020 at 11:12
2
  • 1
    tried the same, but it did not work. I even changed the syntax to pinMode(18, OUTPUT) as pin 30 gpio 18. If you do have an ESP32, can you try it too? Commented Apr 14, 2020 at 11:42
  • 1
    thanks - ive tried changing clock speeds too with no luck. Were you able to try it? Commented Apr 14, 2020 at 14:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.