User Tools

Site Tools


arduino:spi-test-controller

Testing Arduino as SPI with two different Arduino-based peripherals. Use with spi-test-peripheral-even and spi-test-peripheral-odd counter.

spi-test-controller.ino
/**
 * Controller for testing two SPI peripherals, one that returns odd numbers
 * and the other that retuns evens.
 * 
 * Mithat Konar
 * GPL3
 */
 
#include <Arduino.h>
#include <SPI.h>
 
#define ODD_CS_PIN 9
// even CS pin is the predefined SS
 
// === function headers ===
byte SPItransfer(const byte);
 
// === global variables ===
unsigned int  active_SPI_pin;  // state variable for the selected SPI device
SPISettings spiSettings(400000, MSBFIRST, SPI_MODE0);
 
void setup(void)
{
  Serial.begin(115200);
  Serial.println();
 
  pinMode(SS, OUTPUT);     // Even number generator
  digitalWrite(SS, HIGH);  // ibid.
 
  pinMode(ODD_CS_PIN, OUTPUT);     // Odd number generator
  digitalWrite(ODD_CS_PIN, HIGH);  // ibid.
 
  Serial.println("Ready?");
  delay(3000); // time to set up Serial monitor or whatever
  Serial.println("Here we go ...");
 
  SPI.begin();
 
  // always reset counters before first use
  // even counter:
  SPI.beginTransaction(spiSettings);
  digitalWrite(SS, LOW);  // activate even counter
  SPItransfer('0');       // send command for resetting
  digitalWrite(SS, HIGH); // disable
  SPI.endTransaction();
 
  // odd counter:
  SPI.beginTransaction(spiSettings);
  digitalWrite(ODD_CS_PIN, LOW);  // activate odd counter
  SPItransfer('0');               // send command for resetting
  digitalWrite(ODD_CS_PIN, HIGH); // disable
  SPI.endTransaction();  
 
  // let's start by accessing the even counter
  active_SPI_pin = SS;
}
 
void loop(void)
{
  byte num;
 
  Serial.print("\n---");
  if (active_SPI_pin == SS)
    Serial.print(" even ");
  else
    Serial.print(" odd ");
  Serial.println("---");
 
  // get a number from the active number generator
  SPI.beginTransaction(spiSettings);
  digitalWrite(active_SPI_pin, LOW);  // activate!
 
  num = SPItransfer('i');     // get current count (0 or 1), then increment counter.
  Serial.print("'i': ");
  Serial.println(num, DEC);
 
  num = SPItransfer('i');     // get current count (2 or 3), then increment counter.
  Serial.print("'i': ");
  Serial.println(num, DEC);
 
  num = SPItransfer('r');     // get current count (4 or 5).
  Serial.print("'r': ");
  Serial.println(num, DEC);
 
  num = SPItransfer('r');     // get current count (still 4 or 5).
  Serial.print("'r': ");
  Serial.println(num, DEC);
 
  num = SPItransfer('0');     // reset.
  Serial.print("'0': ");      // (still 4 or 5).
  Serial.println(num, DEC);
 
  num = SPItransfer('r');     // now 0 or 1.
  Serial.print("'r': ");
  Serial.println(num, DEC);
 
  digitalWrite(active_SPI_pin, HIGH); // disable
  SPI.endTransaction();
 
  // toggle desired SPI device
  active_SPI_pin = (active_SPI_pin == SS) ? ODD_CS_PIN: SS;
 
  delay(2000);  // 2 second delay 
}  // end of loop
 
// === Utilities ===
/**
 * Send a byte to the selected SPI peripheral and return its response.
 * 
 * @param data the byte to send to the peripheral
 * @return     the peripheral's response (potentially undefined if the
 *             peripheral doesn't return a value.
 */
byte SPItransfer(const byte data)
{
  byte rv = SPI.transfer(data);
  delayMicroseconds(1);
  return rv;
}
arduino/spi-test-controller.txt · Last modified: 2023/07/26 02:22 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki