Hi,
The following is to ask about a speed up for looping over transfer16() with some other operations in between calls to transfer16(). The essential is that there seems to be about 150 nsec between calling transfer16() and the start of the transfer. I would like, in the case of a loop, to reduce that as much as feasible.
For a little bit of context, here is an except with some simplification, from some working code. It synchronizes to a clock, emits a pulse, and then call SPIN.transfer16(0xFFFF)
There is a bout 150 nsecs between the pulse and the start of the SPI clock. The goal is to reduce that.
Code:
#include "Arduino.h"
#include <SPI.h>
#define READPINA (CORE_PIN5_PINREG & CORE_PIN5_BITMASK)
#define SETPINB (CORE_PIN6_PORTSET = CORE_PIN6_BITMASK)
#define CLEARPINB (CORE_PIN6_PORTCLEAR = CORE_PIN6_BITMASK)
SPISettings spi_settings( 30000000, MSBFIRST, SPI_MODE0); // 30 MHz, reads
void setup() {
pinMode(4, OUTPUT); // Clock for the CCD
pinMode(5, INPUT); // Jumpered to pin 4
pinMode(6, OUTPUT); // CNVRT to the ADC
analogWriteResolution(4); // pwm range 4 bits, i.e. 2^4
analogWriteFrequency(4, 600000);
analogWrite(4,8); // dutycycle 50% for 2^4
SPI.begin(); // ADC connected to SPI
SPI.beginTransaction(spi_settings);
}
void loop() {
// other stuff, not relevant
// the readout
for (i=0; i<NREADOUT; i++){
while ( READPINA ) {} // wait while high
while ( !READPINA ) {} // wait while low
SETPINB;
delayNanoseconds( 710 );
CLEARPINB;
*p16++ = SPI.transfer16(0xFFFF);
}
}
For the Teensy 4.0, the source for SPI.transfer16() is as follows (arduino-1.8.9):
Code:
uint16_t transfer16(uint16_t data) {
port().TDR = data; // output 16 bit data.
while ((port().RSR & LPSPI_RSR_RXEMPTY)) ; // wait while the RSR fifo is empty...
port().TCR = tcr; // restore back
return port().RDR;
}
What I want to ask is, can I do the following (i.e. is it likely to work), and where (in source code) does port() come from?
Code:
void loop() {
uint32_t tcr;
tcr = port().TCR;
port().TCR = (tcr & 0xfffff000) | LPSPI_TCR_FRAMESZ(15); // turn on 16 bit mode
for (i=0; i<NREADOUT; i++){
while ( READPINA ) {} // wait while high
while ( !READPINA ) {} // wait while low
SETPINB;
delayNanoseconds( 710 );
CLEARPINB;
port().TDR = 0xFFFF;
while ((port().RSR & LPSPI_RSR_RXEMPTY));
*p16++ = port().RDR;
}
port().TCR = tcr; // restore back
Thank you