Hey all -
I'm working on having a maple drive an arduino through SPI, but am getting lots of/odd errors . I've got Arduino<>Arduino SPI communication down with a only few errors (i.e maybe a single error for several hundred+ transmissions) following http://www.gammon.com.au/forum/?id=10892. When I run the same thing between the maple and arduino (ensuring that the SPI settings are the same), stuff transmits, but I'm getting about 3 or 4 good transmissions, a big several second delay, then some garbage, then the whole cycle repeats. This occurs regardless of the delay I use between the Master(maple) transmitting. I also tried out all of the possible SPI-clockspeed/mode combinations. Any thoughts/ideas?
I should mention - everything is directly wired from maple<>arduino, with nothing else. Should I try adding bypass caps/low-pass-filters? If so, not sure where/how I should place 'em...
Thanks!
Here's my maple code:
#include <SPI.h>
HardwareSPI spi(1);
void setup (void)
{
pinMode(ss1,OUTPUT);
digitalWrite(ss1, HIGH); // ensure SS stays high for now
// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
spi.begin(SPI_1_125MHZ, MSBFIRST, 0);
} // end of setup
void loop(void)
{
dostuff();
delay(3000);
}
void dostuff(){
char c;
for (const char * p = "ping\n" ; c = *p; p++){
spi.write(c);
}
}
Here's my arduino code:
#include <SPI.h>
char buf [100];
volatile byte pos;
volatile boolean process_it;
void setup (void)
{
Serial.begin (19200); // debugging
// have to send on master in, *slave out*
pinMode(MISO, OUTPUT);
// turn on SPI in slave mode
SPCR |= _BV(SPE);
// get ready for an interrupt
pos = 0; // buffer empty
process_it = false;
// now turn on interrupts
SPI.attachInterrupt();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
} // end of setup
// SPI interrupt routine
ISR (SPI_STC_vect)
{
byte c = SPDR; // grab byte from SPI Data Register
// add to buffer if room
if (pos < sizeof buf)
{
buf [pos++] = c;
// example: newline means time to process buffer
if (c == '\n')
process_it = true;
} // end of room available
} // end of interrupt routine SPI_STC_vect
// main loop - wait for flag set in interrupt routine
void loop (void)
{
if (process_it)
{
buf [pos] = 0;
Serial.println (buf);
pos = 0;
process_it = false;
} // end of flag set
} // end of loop