Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1702 - 1
#include "WProgram.h"
2
#include "Spi.h"
3
 
4
//---------- constructor ----------------------------------------------------
5
 
6
SPI::SPI()
7
{
8
  // initialize the SPI pins
9
  pinMode(SCK_PIN, OUTPUT);
10
  pinMode(MOSI_PIN, OUTPUT);
11
  pinMode(MISO_PIN, INPUT);
12
  pinMode(SS_PIN, OUTPUT); // <------- !!! (Remember! This pin will select USB host chip Max3421)
13
 
14
  // enable SPI Master, MSB, SPI mode 0, FOSC/4
15
  mode(0);
16
}
17
 
18
//------------------ mode ---------------------------------------------------
19
 
20
void SPI::mode(byte config)
21
{
22
  byte tmp;
23
 
24
  // enable SPI master with configuration byte specified
25
  SPCR = 0;
26
  SPCR = (config & 0x7F) | (1<<SPE) | (1<<MSTR);
27
  tmp = SPSR;
28
  tmp = SPDR;
29
}
30
 
31
//------------------ transfer -----------------------------------------------
32
 
33
byte SPI::transfer(byte value)
34
{
35
  SPDR = value;
36
  while (!(SPSR & (1<<SPIF))) ;
37
  return SPDR;
38
}
39
 
40
byte SPI::transfer(byte value, byte period)
41
{
42
  SPDR = value;
43
  if (period > 0) delayMicroseconds(period);
44
  while (!(SPSR & (1<<SPIF))) ;
45
  return SPDR;
46
}
47
 
48
 
49
//---------- preinstantiate SPI object --------------------------------------
50
 
51
SPI Spi = SPI();