You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.7 KiB
70 lines
1.7 KiB
/*
|
|
|
|
SPI Configuration
|
|
|
|
Author:
|
|
|
|
Philip Lindsay <follower@rancidbacon.com>
|
|
|
|
License:
|
|
|
|
Copyright 2007-2008 // LGPL
|
|
|
|
|
|
TODO: Enable multiple SPI devices to be controlled.
|
|
|
|
*/
|
|
|
|
#include "spi.h"
|
|
|
|
SpiConfiguration::SpiConfiguration() {
|
|
// begin();
|
|
}
|
|
|
|
void SpiConfiguration::begin(void) {
|
|
/*
|
|
|
|
Configure pins and registers required for SPI communication.
|
|
|
|
*/
|
|
|
|
// Configure I/O pins
|
|
pinMode(PIN_DATA_OUT, OUTPUT);
|
|
pinMode(PIN_DATA_IN, INPUT);
|
|
pinMode(PIN_SPI_CLOCK, OUTPUT);
|
|
pinMode(PIN_SLAVE_SELECT, OUTPUT);
|
|
|
|
digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave
|
|
|
|
/*
|
|
Configure SPI Control Register (SPCR) (All values initially 0)
|
|
Bit Description
|
|
7 SPI Interrupt Enable -- disable (SPIE --> 0)
|
|
6 SPI Enable -- enable (SPE --> 1)
|
|
5 Data Order -- MSB 1st (DORD --> 0) (Slave specific)
|
|
4 Master/Slave Select -- master (MSTR --> 1)
|
|
3 Clock Polarity -- (CPOL --> 0) (Slave specific) ("Mode")
|
|
2 Clock Phase -- (CPHA --> 0) (Slave specific)
|
|
1 SPI Clock Rate Select 1 -- } (SPR1 --> 0)
|
|
0 SPI Clock Rate Select 0 -- } fOSC/4 (SPR0 --> 0) ("Fastest" but see SPI2X in SPSR)
|
|
*/
|
|
SPCR = (1<<SPE)| (1<<MSTR);
|
|
|
|
// Clear previous data and status (TODO: Determine if necessary/better way.)
|
|
// (Based on Arduino Playground SPI example.)
|
|
byte dummy;
|
|
dummy = SPSR;
|
|
dummy = SPDR;
|
|
delay(10);
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
Instantiate a 'SPI' object to be used in a manner consistent with
|
|
the way the Arduino environment handles serial configuration with the
|
|
'Serial'/'HardwareSerial' object/class combination.
|
|
*/
|
|
|
|
SpiConfiguration SPI = SpiConfiguration();
|