Attiny88 as SPI slave

I have programmed Attiny88 with Arduino IDE 1.6.5 using GitHub - SpenceKonde/ATTinyCore: Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8 . I chose Attiny88 because it’s the cheapest Attiny (as low as $0.5) and at the same time has 28 pins with dedicated SPI. I wrote a code for arduino to work as SPI master, and a code for Attiny88 as SPI slave but I get only 0 as answer from Attiny.

SPI master code for ARduino UNO

#include <SPI.h>

// set up the speed, data order and data mode
SPISettings settingsA(2000000, MSBFIRST, SPI_MODE0); 
const int slaveAPin = 10;

void setup() {
  // set the Slave Select Pins as outputs:
  Serial.begin(9600);
  Serial.println("SPI ON"); 
  pinMode (slaveAPin, OUTPUT);
  SPI.begin(); 
}

uint8_t stat, val1, result;

void loop() {
   // read two bytes from device A
   SPI.beginTransaction(settingsA);
   digitalWrite (slaveAPin, LOW);
   // reading only, so data sent does not matter
  stat = SPI.transfer(0);
  val1 = SPI.transfer(0);
  digitalWrite (slaveAPin, HIGH);
  SPI.endTransaction();
  Serial.println("data");
  Serial.println(stat); 
  Serial.println(val1);
  Serial.println("end");    
  SPI.endTransaction();
  delay(300);
}

SPI slave code for Attiny88:

#include <SPI.h>

// set up the speed, data order and data mode
SPISettings settingsA(2000000, MSBFIRST, SPI_MODE0); 
const int slavePin = 14;
const int ledPin = 9;

uint8_t stat, val1, val2, result;

void setup() {
  // set the Slave Select Pins as outputs:
  pinMode (slavePin, INPUT);
  pinMode (ledPin, OUTPUT);
  // initialize SPI:
  digitalWrite(ledPin, HIGH);
  delay(1000);
  SPI.begin();
  stat =128;
  val1= 1;
}

void loop() {

  if (slavePin==LOW) {
    SPI.beginTransaction(settingsA);    
    SPI.transfer(stat);
    SPI.transfer(val1);
    result = SPI.transfer(0);
    digitalWrite(ledPin, result);
    SPI.endTransaction();
  }

}

samakar:
I have programmed Attiny88 with Arduino IDE 1.6.5 using GitHub - SpenceKonde/ATTinyCore: Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8 . I chose Attiny88 because it’s the cheapest Attiny (as low as $0.5) and at the same time has 28 pins with dedicated SPI. I wrote a code for arduino to work as SPI master, and a code for Attiny88 as SPI slave but I get only 0 as answer from Attiny.

SPI slave code for Attiny88:

#include <SPI.h>

// set up the speed, data order and data mode
SPISettings settingsA(2000000, MSBFIRST, SPI_MODE0);
const int slavePin = 14;
const int ledPin = 9;

You are confused on your pin numbers. Adruino (slavePIN)=14 is not correct. The SS pin as defined in the pins_arduino.h for the ATtiny series variant tinyX8 is Arduino digital 10.

So, The SPI hardware is waiting on digital pin (10) to go low as an input. You have not configured it as an input, therefore The AtTiny88 is in master mode.

change your definition for slavePin to:

const int slavePin = SS;  // this uses the predefined value for SS for the processor selected 
  // SS for the tinyX8 variant is chip pin 14, but the ARDUINO IDE - compiler maps 10 to the physical pin 14.

samakar:

void loop() {

if (slavePin==LOW) {
   SPI.beginTransaction(settingsA);    
   SPI.transfer(stat);
   SPI.transfer(val1);
   result = SPI.transfer(0);
   digitalWrite(ledPin, result);
   SPI.endTransaction();
 }

}

Your loop will not do what you want.

Slave mode SPI transfers are not supported by the SPI library. You will have to write your own.

The SPI.begin() call initialized the hardware as a SPI Master (driving CLK, MOSI is output, MISO is input, SS is OUTPUT).

I wrote an interrupt driven Master Mode library, I am still working on the Slave Mode.

Chuck.


Check out my Kickstarter Project Memory Panes an expansion RAM Shield for Mega2560’s. It adds 1MB of RAM for those projects where 8KB is not enough.