| 1 | /* |
|---|
| 2 | |
|---|
| 3 | SPI Configuration |
|---|
| 4 | |
|---|
| 5 | Author: |
|---|
| 6 | |
|---|
| 7 | Philip Lindsay <follower@rancidbacon.com> |
|---|
| 8 | |
|---|
| 9 | License: |
|---|
| 10 | |
|---|
| 11 | Copyright 2007-2008 // LGPL |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | TODO: Enable multiple SPI devices to be controlled. |
|---|
| 15 | |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | #include "spi.h" |
|---|
| 19 | |
|---|
| 20 | SpiConfiguration::SpiConfiguration() { |
|---|
| 21 | // begin(); |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | void SpiConfiguration::begin(void) { |
|---|
| 25 | /* |
|---|
| 26 | |
|---|
| 27 | Configure pins and registers required for SPI communication. |
|---|
| 28 | |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | // Configure I/O pins |
|---|
| 32 | pinMode(PIN_DATA_OUT, OUTPUT); |
|---|
| 33 | pinMode(PIN_DATA_IN, INPUT); |
|---|
| 34 | pinMode(PIN_SPI_CLOCK, OUTPUT); |
|---|
| 35 | pinMode(PIN_SLAVE_SELECT, OUTPUT); |
|---|
| 36 | |
|---|
| 37 | digitalWrite(PIN_SLAVE_SELECT, HIGH); // Disable slave |
|---|
| 38 | |
|---|
| 39 | /* |
|---|
| 40 | Configure SPI Control Register (SPCR) (All values initially 0) |
|---|
| 41 | Bit Description |
|---|
| 42 | 7 SPI Interrupt Enable -- disable (SPIE --> 0) |
|---|
| 43 | 6 SPI Enable -- enable (SPE --> 1) |
|---|
| 44 | 5 Data Order -- MSB 1st (DORD --> 0) (Slave specific) |
|---|
| 45 | 4 Master/Slave Select -- master (MSTR --> 1) |
|---|
| 46 | 3 Clock Polarity -- (CPOL --> 0) (Slave specific) ("Mode") |
|---|
| 47 | 2 Clock Phase -- (CPHA --> 0) (Slave specific) |
|---|
| 48 | 1 SPI Clock Rate Select 1 -- } (SPR1 --> 0) |
|---|
| 49 | 0 SPI Clock Rate Select 0 -- } fOSC/4 (SPR0 --> 0) ("Fastest" but see SPI2X in SPSR) |
|---|
| 50 | */ |
|---|
| 51 | SPCR = (1<<SPE)| (1<<MSTR); |
|---|
| 52 | |
|---|
| 53 | // Clear previous data and status (TODO: Determine if necessary/better way.) |
|---|
| 54 | // (Based on Arduino Playground SPI example.) |
|---|
| 55 | byte dummy; |
|---|
| 56 | dummy = SPSR; |
|---|
| 57 | dummy = SPDR; |
|---|
| 58 | delay(10); |
|---|
| 59 | |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | /* |
|---|
| 64 | Instantiate a 'SPI' object to be used in a manner consistent with |
|---|
| 65 | the way the Arduino environment handles serial configuration with the |
|---|
| 66 | 'Serial'/'HardwareSerial' object/class combination. |
|---|
| 67 | */ |
|---|
| 68 | |
|---|
| 69 | SpiConfiguration SPI = SpiConfiguration(); |
|---|