Hi all!
I'm using Teensy 3.1 running at 72MHz to attempt to man-in-the-middle SPI communication between another two chips. Specifically, I have the other MCU's (master) CLK/EN lines hooked up to the Teensy and slave, and have pin 9 on the Teensy hooked up as the MOSI to the slave. I'm trying to have the Teensy send data to the slave, but have the other MCU control the timing. This is a bare bones test as the software will get more advanced shortly. I was thinking of using hardware SPI, however my future intentions are to change bits on the fly from the original MOSI, so I may need to avoid it.
The issue I'm experiencing is the MCU's SPI clock has a duty cycle of 1.04us, and it seems that either interrupts aren't fast enough or I'm doing something else wrong because I can't have the Teensy keep up in the most bare bones configuration. Is this simply the number of clock cycles some of the code is taking? I tested digitalWriteFast() and found that it's 20ns on my Teensy which is great but for a small function it seems to take 1540ns to perform a digitalWriteFast and a few more things.
Here is an image of a sample of the pins through a logic analyzer and the associated code (a lot of the code is repeated simply to get rid of functions and try to execute things as quickly as possible)
In this image, CLK is the MCU SPI clock (attached to pin 14 of Teensy), RSSI is the DATA_OUT pin of Teensy (pin 9), and EN is the SPI EN pin from the MCU (pin 10 on Teensy). It appears that it takes 200ns from when CLK goes low to hit the interrupt, call clockChange(), and change the bit. Should it be taking 1540
Code:
I'm using Teensy 3.1 running at 72MHz to attempt to man-in-the-middle SPI communication between another two chips. Specifically, I have the other MCU's (master) CLK/EN lines hooked up to the Teensy and slave, and have pin 9 on the Teensy hooked up as the MOSI to the slave. I'm trying to have the Teensy send data to the slave, but have the other MCU control the timing. This is a bare bones test as the software will get more advanced shortly. I was thinking of using hardware SPI, however my future intentions are to change bits on the fly from the original MOSI, so I may need to avoid it.
The issue I'm experiencing is the MCU's SPI clock has a duty cycle of 1.04us, and it seems that either interrupts aren't fast enough or I'm doing something else wrong because I can't have the Teensy keep up in the most bare bones configuration. Is this simply the number of clock cycles some of the code is taking? I tested digitalWriteFast() and found that it's 20ns on my Teensy which is great but for a small function it seems to take 1540ns to perform a digitalWriteFast and a few more things.
Here is an image of a sample of the pins through a logic analyzer and the associated code (a lot of the code is repeated simply to get rid of functions and try to execute things as quickly as possible)
In this image, CLK is the MCU SPI clock (attached to pin 14 of Teensy), RSSI is the DATA_OUT pin of Teensy (pin 9), and EN is the SPI EN pin from the MCU (pin 10 on Teensy). It appears that it takes 200ns from when CLK goes low to hit the interrupt, call clockChange(), and change the bit. Should it be taking 1540
Code:
Code:
#define DATA_OUT 9
#define CLK 14
#define EN 10
volatile int outBits;
volatile byte clkstate;
volatile byte enstate;
volatile byte nextBit;
void setup()
{
pinMode(CLK, INPUT);
pinMode(EN, INPUT);
pinMode(DATA_OUT, OUTPUT);
attachInterrupt(digitalPinToInterrupt(CLK), clockChange, CHANGE);
attachInterrupt(digitalPinToInterrupt(EN), spiChange, LOW);
enstate = digitalReadFast(EN);
clkstate = digitalReadFast(CLK);
}
void loop()
{
}
// EN went low, send the first bit
void spiChange()
{
enstate = !enstate;
// if EN goes LOW (data coming in)
if (enstate == LOW)
{
digitalWriteFast(DATA_OUT, nextBit);
nextBit = outBits & 1;
outBits >>= 1;
}
else
{
outBits = 0x5555;
nextBit = outBits & 1;
outBits >>= 1;
}
}
void clockChange()
{
// if EN and clock goes low, load in new bit
if (clkstate == !LOW)
{
digitalWriteFast(DATA_OUT, nextBit);
nextBit = outBits & 1;
outBits >>= 1;
}
clkstate = !clkstate;
}