Statistics
| Branch: | Revision:

root / arduino-1.0 / libraries / SPI / SPI.cpp @ 58d82c77

History | View | Annotate | Download (1.47 KB)

1
/*
2
 * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
3
 * SPI Master library for arduino.
4
 *
5
 * This file is free software; you can redistribute it and/or modify
6
 * it under the terms of either the GNU General Public License version 2
7
 * or the GNU Lesser General Public License version 2.1, both as
8
 * published by the Free Software Foundation.
9
 */
10

    
11
#include "pins_arduino.h"
12
#include "SPI.h"
13

    
14
SPIClass SPI;
15

    
16
void SPIClass::begin() {
17
  // Set direction register for SCK and MOSI pin.
18
  // MISO pin automatically overrides to INPUT.
19
  // When the SS pin is set as OUTPUT, it can be used as
20
  // a general purpose output port (it doesn't influence
21
  // SPI operations).
22

    
23
  pinMode(SCK, OUTPUT);
24
  pinMode(MOSI, OUTPUT);
25
  pinMode(SS, OUTPUT);
26
  
27
  digitalWrite(SCK, LOW);
28
  digitalWrite(MOSI, LOW);
29
  digitalWrite(SS, HIGH);
30

    
31
  // Warning: if the SS pin ever becomes a LOW INPUT then SPI 
32
  // automatically switches to Slave, so the data direction of 
33
  // the SS pin MUST be kept as OUTPUT.
34
  SPCR |= _BV(MSTR);
35
  SPCR |= _BV(SPE);
36
}
37

    
38
void SPIClass::end() {
39
  SPCR &= ~_BV(SPE);
40
}
41

    
42
void SPIClass::setBitOrder(uint8_t bitOrder)
43
{
44
  if(bitOrder == LSBFIRST) {
45
    SPCR |= _BV(DORD);
46
  } else {
47
    SPCR &= ~(_BV(DORD));
48
  }
49
}
50

    
51
void SPIClass::setDataMode(uint8_t mode)
52
{
53
  SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
54
}
55

    
56
void SPIClass::setClockDivider(uint8_t rate)
57
{
58
  SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
59
  SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
60
}
61