HSPI don't generate SCLK or MOSI signals

robbinPAH
Posts: 1
Joined: Fri Aug 25, 2023 8:47 am

HSPI don't generate SCLK or MOSI signals

Postby robbinPAH » Fri Aug 25, 2023 8:56 am

I wrote the code as below:
MPU is ESP32 devkit-C and AD converter is MCP3208.
I want to use HSPI. However, I cannot observe any signals from SCLK and MOSI. (The oscilloscope is made of Tektronix)
Is there something wrong?? I can't find it.
  1. #define ADC_CS  25
  2. #define MISO    12
  3. #define MOSI    13
  4. #define SCLK    14
  5. #define   SS    15
  6.  
  7. SPIClass hspi(HSPI);
  8. SPISettings settings = SPISettings(400000, MSBFIRST, SPI_MODE0);
  9.  
  10. void InitializeSPI()
  11. {
  12. //SPI
  13.   hspi.begin(SCLK, MISO, MOSI, SS);
  14.  
  15.   pinMode(ADC_CS, OUTPUT);
  16.   pinMode(  SCLK, OUTPUT);
  17.   pinMode(  MISO,  INPUT);
  18.   pinMode(  MOSI, OUTPUT);
  19.   pinMode(    SS, OUTPUT);
  20.  
  21.   digitalWrite(ADC_CS, HIGH);
  22. }
  23.  
  24. unsigned short ReadADC(char ch)
  25. {
  26.   hspi.beginTransaction(settings);
  27.   digitalWrite(ADC_CS, LOW);
  28.     hspi.transfer(0x06 | ch >> 2);
  29.     uint8_t highByte = hspi.transfer((ch & 0x03) << 6); // singleEnd
  30.     uint8_t  lowByte = hspi.transfer(0x00); // dummy
  31.   digitalWrite(ADC_CS, HIGH);
  32.   hspi.endTransaction();
  33.  
  34.   unsigned short adc = ((highByte & 0x0f) << 8) + lowByte;
  35.   Serial.println("result value = " + String(adc, DEC));
  36.  
  37.   return adc;
  38. }

MicroController
Posts: 1707
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: HSPI don't generate SCLK or MOSI signals

Postby MicroController » Sat Aug 26, 2023 2:01 pm

I don't really know Arduino, but could it be that pinMode(...) implicitly (re-)configures a pin to function as GPIO? In your case, that would happen after hspi.begin(...) configured the pins to work as SPI pins routed to the SPI peripheral.
Check if it works if you omit pinMode(...) for the SPI pins.

Also, the order of

Code: Select all

  digitalWrite(ADC_CS, HIGH);
  hspi.endTransaction();
should be turned around.

Who is online

Users browsing this forum: No registered users and 79 guests