I am using a teensy 3.1 to toggle a GPIO (pin 0) as fast as possible. I have noticed that there is considerable jitter in the pulses which I measured to be ~350-440ns. If I had to guess, there is some kind of background interrupt running on the Teensy (an interrupt I did not set up).
I do not need anything besides this GPIO pin and the SPI bus (interrupts enabled for receiving in slave mode using btmcmahan's SPI library). Is there a way to disable any background interrupts? Ideally, the only jitter/interrupt should be is when the SPI receives data (which I measured is ~1.2 us).
Code below:
I do not need anything besides this GPIO pin and the SPI bus (interrupts enabled for receiving in slave mode using btmcmahan's SPI library). Is there a way to disable any background interrupts? Ideally, the only jitter/interrupt should be is when the SPI receives data (which I measured is ~1.2 us).
Code below:
Code:
#include "t3spi.h" // Library code to use the hardware SPI in slave mode
T3SPI SPI_SLAVE; //initalise the T3SPI class as SPI_SLAVE
#define dataLength 2 //only two integer per SPI packet
volatile uint16_t data[dataLength] = {}; //initalise empty data array
volatile uint16_t delayCount = 15; //initalise the delay counter (time the pulse stays LOW)
volatile uint16_t pulseCount = 15; //initalise the pulse counter (time the pulse stays HIGH)
uint16_t Tpulse = 0; //temporary pulse counter
uint16_t Tduty = 0; //temporary delay counter
byte initalised = 0; //the pulses are OFF until it gets a command from SPI
void setup() {
pinMode(0,OUTPUT); // set the TTL output as an output (pin 0)
digitalWriteFast(0,LOW); //set the TTL low
pinMode(1,OUTPUT); // used for measuring the SPI ISR time
digitalWriteFast(1,LOW); //turn it off for now.
//Begin the SPI in SLAVE:
SPI_SLAVE.begin_SLAVE(SCK, MOSI,MISO,CS0); //pin 13,11,12,10 respectively
SPI_SLAVE.setCTAR_SLAVE(16, SPI_MODE0); //16 bit frame size, SPI mode 0
NVIC_ENABLE_IRQ(IRQ_SPI0); //Enable the interrupt for SPI0
}
void loop() {
while(initalised){
//this loop modifies pin 0 (laser TTL) as fast as possible
Tpulse = pulseCount; //set the temporary pulse couter
Tduty = delayCount; //set the temporary duty counter
digitalWriteFast(0,HIGH); //ON
while (Tpulse != 0){ //wait until the pulse counter reaches zero
Tpulse--; //decrement the pulse counter
}
digitalWriteFast(0,LOW); //OFF
while (Tduty != 0){ //wait until the delay counter reaches zero
Tduty--; //decrement the delay counter
}
}
}
// ISR for the SPI0 interface
void spi0_isr(void){
digitalWriteFast(0,LOW); //make sure the laser is off
digitalWriteFast(1,HIGH); //Set this pin high to measure the computation time for this ISR
initalised = 1;
//Handles the data transfer:
// get the data from the buffer:
SPI_SLAVE.rx16(data, dataLength);
//split up the data into the two types:
delayCount = data[1] & 0x03FF; //delay count is the lower 10 bytes
pulseCount = (data[1] >> 12) & 0x000F; //pulse count is the upper 4 bytes
digitalWriteFast(1,LOW); //end of the ISR, reset the debug pin to zero.
}