Start and Stop SPI using beginTransaction


Im using the SAMD21G or rather the SAMW25 (MKR1000).

Im trying to get some SPI devices talk to me. I tried it “the old fashioned way” (?) using spi.begin and it worked.As I tried to optimize it, I found the guide for the SPI Library here:

Its mentioned to start the SPI transaction using SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0)); and end it with SPI.endTransaction()

but that doesnt work for me at all!

My code is pretty simple (just for testing purpose)

double MCP3208::readADC(char adChannel)
  uint32_t oversampleRead = 0;
  uint16_t intResult = 0;
  // enable ADC SPI
  //Select chip by pulling down CS
  SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0)); //Set SPI Speed
for (int i=0; i < _oversamplingRate; i++){
  digitalWrite(_csPin, LOW);
  // compute 16 bit value for channel and shift into position
  int tInt = (adChannel + 24) << 6;

  // transfer the high byte

  // transfer the low byte and get high 4 bits
  byte hiRes = SPI.transfer(lowByte(tInt));

  // get the low 8 bits
  byte lowRes = SPI.transfer(0);
  hiRes = hiRes & B00001111;

  //Combine to make 12 bit value
  oversampleRead +=  (hiRes << 8 | lowRes);

    // disable ADC SPI
  digitalWrite(_csPin, HIGH);
  intResult = oversampleRead / _oversamplingRate ;
  AddToMovingAvg(&filteredChannel[adChannel], intResult);
  return intResult;

When I swap the SPI.beginTransaction(…) with the simple SPI.begin() it is working, otherwise I dont get any SPI on this Device (verified using the oscilloscope). Will the “SPI Session” stop if I use only begin and then endTransaction at the end?

What are the differences between beginTransaction und begin only?


What are the differences between beginTransaction und begin only?

Did you look at the reference page? begin() and beginTransaction() do completely different things. One gets the hardware ready. The other gets the bus going the proper way.

Well.. Yes, I did but itis not really clear to me since .begin() is never mentioned in the SPI reference ( SPI reference )

In the reference it says

Once you have your SPI parameters, use SPI.beginTransaction() to begin using the SPI port. The SPI port will be configured with your all of your settings.

beginTransaction() Description

Initializes the SPI bus using the defined SPISettings.

begin() Description

Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.

I read some Thread and found that you have to use both, so I got it working for me which is nice.


begin() is never mentioned in the SPI reference

It is used in the examples, though.