Pages: [1]   Go Down
Author Topic: adding slave support to SPI library  (Read 526 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
Karma: 0
Posts: 57
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Offline Offline
Shannon Member
Karma: 207
Posts: 12200
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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 ]

Pages: [1]   Go Up
Jump to: