Teensy 4.0 and SPI - problems with number of transactions in an interrupt routine

Status
Not open for further replies.

rt54321

Member
Greetings All!

I'm using the Teensy 4.0 with an external ADC (the ADS8861, which is a 16 bit, SPI output ADC). I'm in the testing phase, and I'm getting erroneous outputs on the SPI data and clock lines.

The yellow trace is the "conversion start" pin. I'm triggering ADC conversions at 10kHz. The blue trace is the SPI clock line. My (abbreviated) code is here:

Code:
// set up the speed, mode and endianness of SPI devices:
SPISettings settingsADC(1000000, MSBFIRST, SPI_MODE2);  //SPI_MODE2?

void setup()   {                
  Serial.begin(9600);
  pinMode(adcTriggerPin,OUTPUT);
  analogWriteFrequency( adcTriggerPin, 10000);  
  pinMode(adcReadyPin, INPUT);
  SPI.begin();
  attachInterrupt(adcReadyPin, ADC_ready, RISING);
  analogWrite(adcTriggerPin, 150); //Start the ADC converstion trigger
}
void loop()                     
{
    delay(1000);
}
void ADC_ready(void)
{
    noInterrupts(); //Disable interrupts    
    interrupt_counter++;
    for (volatile int time_waster = 0; time_waster < 100; time_waster++)
    {  volatile int j = time_waster + 1; } 
    SPI.beginTransaction(settingsADC);
    ADCbyte1 = SPI.transfer(0);
    ADCbyte2 = SPI.transfer(0);
    ADCresult = (int32_t)((ADCbyte1 << 8) + ADCbyte2);    // * 5.0f / 32768.0f;
    SPI.endTransaction();
    interrupts(); //Enable interrupts
}

When I debug my ADC result, I get an erroneous value of ADCresult = 65535. Here's the question: In my interrupt, I call on 2 SPI.transfer() transactions (8 bits + 8 bits = 16 bits in total, for the ADC result). In the oscilloscope capture, there are 3 blocks of SPI clocks (the blue trace). This tells me that 3 SPI transactions are happening, even though I only called for 2. How can this happen? I'm not sure why my SPI is "triggering" too many times.

DS0007.png
 
Status
Not open for further replies.
Back
Top