Fluffy
Member
Hey guys,
So I've been stuck on this issue for quite a while. I've been trying to figure out how this library works:
I found a few issues with the header and found a solution here:
I ran the following code on my master:
Ran this in the slave:
Got this in the serial monitor:
I expected the same data to be received in the slave, and instead, I received 36555. What am I doing wrong?
So I've been stuck on this issue for quite a while. I've been trying to figure out how this library works:
HTML:
https://github.com/btmcmahan/Teensy-3.0-SPI-Master---Slave
I found a few issues with the header and found a solution here:
HTML:
https://github.com/soerup/Teensy-3.0-SPI-Master---Slave/commit/18efd0fbef6a444e74da32d6bd09aa28adf8a97e
I ran the following code on my master:
Code:
#include <t3spi.h>
//Initialize T3SPI class as SPI_MASTER
T3SPI SPI_MASTER;
//The number of integers per data packet
#define dataLength 256
//Initialize the arrays for outgoing data
//volatile uint8_t data[dataLength] = {};
volatile uint16_t data[dataLength] = {};
void setup(){
Serial.begin(115200);
//Begin SPI in MASTER (SCK pin, MOSI pin, MISO pin, CS pin, Active State)
SPI_MASTER.begin_MASTER(SCK, MOSI, MISO, CS1, CS_ActiveLOW);
//Set the CTAR (CTARn, Frame Size, SPI Mode, Shift Mode, Clock Divider)
//SPI_MASTER.setCTAR(CTAR1,8,SPI_MODE0,LSB_FIRST,SPI_CLOCK_DIV4);
SPI_MASTER.setCTAR(CTAR0,16,SPI_MODE0,LSB_FIRST,SPI_CLOCK_DIV2);
//Populate data array
for (int i=0; i<dataLength; i++){
data[i]=i;}
//Wait for Slave
delay(5000);
}
void loop(){
//Capture the time before sending data
SPI_MASTER.timeStamp1 = micros();
//Send n number of data packets
for (int i=0; i<1; i++) {
//Send data (data array, data array length, CTARn, CS pin)
//SPI_MASTER.tx8(data, dataLength,CTAR1,CS1);}
SPI_MASTER.tx16(data, dataLength,CTAR0,CS1);}
//Capture the time when transfer is done
SPI_MASTER.timeStamp2 = micros();
//Print data sent & data received
for (int i=0; i<dataLength; i++){
Serial.print("data[");
Serial.print(i);
Serial.print("]: ");
Serial.println(data[i]);
Serial.flush();}
//Print statistics for the previous transfer
SPI_MASTER.printStatistics(dataLength);
//Reset the packet count
SPI_MASTER.packetCT=0;
delay(2000);
}
Ran this in the slave:
Code:
#include <t3spi.h>
//Initialize T3SPI class as SPI_SLAVE
T3SPI SPI_SLAVE;
//The number of integers per data packet
//MUST be the same as defined on the MASTER device
#define dataLength 256
//Initialize the arrays for incoming data
//volatile uint8_t data[dataLength] = {};
volatile uint16_t data[dataLength] = {};
void setup(){
Serial.begin(115200);
//Begin SPI in SLAVE (SCK pin, MOSI pin, MISO pin, CS pin)
SPI_SLAVE.begin_SLAVE(SCK, MOSI, MISO, CS0);
//Set the CTAR0_SLAVE0 (Frame Size, SPI Mode)
//SPI_SLAVE.setCTAR_SLAVE(8, SPI_MODE0);
SPI_SLAVE.setCTAR_SLAVE(16, SPI_MODE0);
//Enable the SPI0 Interrupt
NVIC_ENABLE_IRQ(IRQ_SPI0);
}
void loop(){
//Capture the time before receiving data
if (SPI_SLAVE.dataPointer==0 && SPI_SLAVE.packetCT==0){
SPI_SLAVE.timeStamp1=micros();}
//Capture the time when transfer is done
if (SPI_SLAVE.packetCT==1){
SPI_SLAVE.timeStamp2=micros();
//Print data received & data sent
for (int i=0; i<dataLength; i++){
Serial.print("data[");
Serial.print(i);
Serial.print("]: ");
Serial.println(data[i]);
Serial.flush();}
//Print statistics for the previous transfer
SPI_SLAVE.printStatistics(dataLength);
//Reset the packet count
SPI_SLAVE.packetCT=0;}
}
//Interrupt Service Routine to handle incoming data
void spi0_isr(void){
//Function to handle data
//SPI_SLAVE.rx8 (data, dataLength);
SPI_SLAVE.rx16(data, dataLength);
}
Got this in the serial monitor:
data[0]: 36555
data[1]: 36555
data[2]: 36555
data[3]: 36555
...
data[255]: 36555
SPIO_MCR: 00000000000000000000000000000000
SPIO_CTAR_SLAVE: 01111000000000000000000000000000
SPI0_SR: 01000010000000000000000000000000
SPI0_RSER: 00000000000000100000000000000000
Frame Size: 16
Data Length: 256
Packets: 1
Bytes Sent: 512
Time Elapsed: 291
uSecs/Byte: 0.57
Mbps: 1.76
I expected the same data to be received in the slave, and instead, I received 36555. What am I doing wrong?