For a project I would like to use the SPI bus 1 or 2 as SPI slave and accept commands from the master using the interrupt routine and answer accordingly.
Since the SPI bus 0 of the Teensy is already occupied by an RFID reader (RC255) I have to switch to another SPI bus.
Here is my code, which works on an Arduino UNO without any problems:
How can I get the interrupt routine to use the SPI bus 1 or 2 from the teensy?
Since the SPI bus 0 of the Teensy is already occupied by an RFID reader (RC255) I have to switch to another SPI bus.
Here is my code, which works on an Arduino UNO without any problems:
Code:
#include <SPI.h>
volatile byte answerOne[] = {22,22,16,2,6,2,247,16,3}; //"L" answer
volatile byte answerTwo[] = {22,22,16,2,'i',7,0,0,0,0,1,142,16,3}; // "O" answer
//byte address[8] = {0x28, 0xFF, 0x15, 0xC6, 0x62, 0x15, 0x1, 0x71};
volatile int state_s1 = 0;
volatile int state_prev_s1 = 0;
volatile int answerType = 0;
volatile int counter = 0;
void setup (void)
{
Serial.begin (1000000); // debugging
// turn on SPI in slave mode
SPCR |= bit (SPE);
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// now turn on interrupts
SPI.attachInterrupt();
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR; // grab byte from SPI Data Register
state_prev_s1 = state_s1;
switch (state_s1){
case 0: //START
if(c == 22){
state_s1 = 1;
}
SPDR = 0;
break;
case 1: // 2nd byte
if(c == 22){
state_s1 = 2;
}else{
state_s1 = 0;
}
SPDR = 0;
break;
case 2: // 3rd byte
if(c == 16){
state_s1 = 3;
}else{
state_s1 = 0;
}
SPDR = 0;
break;
case 3: // 4th byte
if(c == 2){
state_s1 = 4;
}else{
state_s1 = 0;
}
SPDR = 0;
break;
case 4: // 5th byte
if(c == 'L'){
answerType = 1;
state_s1 = 5;
}else if(c == 'O'){
answerType = 2;
state_s1 = 5;
}else{
state_s1 = 0;
}
SPDR = 0;
break;
case 5: //1th end byte
if(c == 16){
state_s1 = 6;
}
SPDR = 0;
break;
case 6: // 2nd end byte
if(c == 3){
state_s1 = 7;
}else{
state_s1 = 5;
}
SPDR = 0;
break;
case 7: // Send Answer
if(answerType == 1){
if(answerOne[counter] == 3){
SPDR = answerOne[counter];
state_s1 = 0;
counter = 0;
break;
}
SPDR = answerOne[counter++];
}else if(answerType == 2){
if(answerTwo[counter] == 3){
SPDR = answerTwo[counter];
state_s1 = 0;
counter = 0;
break;
}
SPDR = answerTwo[counter++];
}
testPrint(counter);
break;
}
} // end of interrupt routine SPI_STC_vect
void testPrint(int c){
Serial.print("Question complete! - Answer type=");
Serial.print(answerType);
Serial.print(" - Counter = ");
Serial.println(c);
}
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if(state_prev_s1 != state_s1){
Serial.println(state_s1);
}
} // end of loop
How can I get the interrupt routine to use the SPI bus 1 or 2 from the teensy?