Go Down

Topic: adding slave support to SPI library (Read 566 times) previous topic - next topic

eriknyquist

So I have recently discovered that Arduino SPI library includes no slave support, bit silly, so I thought I'd add it myself. Good way to learn about how the libraries work. I had a quick search around and couldn't find any similar work on the web, but I thought I would ask here since I have access to many Arduino developers and enthusiasts. So, before I get stuck in,

1. is there any specific reason for there being no slave support in the Arduino SPI library that I should know about before I begin (i.e. software limitations)?

2. has anybody already added this support, or perhaps written a new and improved SPI library that I have missed somehow?

MarkT

1) The reason is probably because its rarely used.

2) no, but I've written standalone SPI client proof-of-concept code.
You could add an interrupt handler that uses a callback to get the next response byte:

Code: [Select]

ISR (SPI_STC_vect)
{
  byte to = 0 ;
  while (to < TIMEOUT && !(SPSR & (1 << SPIF)))  // paranoid check for status flag, probably not needed
  {
    to ++ ;
  }
  if (SPSR & (1 << SPIF))
    SPDR = spi_slave_callback (SPDR) ;
}

spi_slave_callback would be a function pointer passed to the library before enabling SPI or slave mode.  You'd need to provide
an initial value to SPDR before enabling slave mode too (the first response byte).
[ I won't respond to messages, use the forum please ]

Go Up