-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPI.cpp
More file actions
87 lines (72 loc) · 2.36 KB
/
Copy pathSPI.cpp
File metadata and controls
87 lines (72 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
* SPI Master library for arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
#include "pins_arduino.h"
#include "SPI.h"
SPIClass SPI;
void SPIClass::begin() {
// Set SS to high so a connected chip will be "deselected" by default
// When the SS pin is set as OUTPUT, it can be used as
// a general purpose output port (it doesn't influence
// SPI operations).
// Set direction register for SCK and MOSI pin.
// MISO pin automatically overrides to INPUT.
// By doing this AFTER enabling SPI, we avoid accidentally
// clocking in a single bit since the lines go directly
// from "input" to SPI control.
// http://code.google.com/p/arduino/issues/detail?id=888
#ifdef __ATTINY
SPI_PORT |= _BV(SPI_PIN); //digitalWrite(SPI_PIN, HIGH);
SPI_DDR |= _BV(SPI_PIN) | _BV(USCK_PIN) | _BV(DO_PIN); //pinMode(USCK_PIN, SPI_PIN, DO_PIN, OUTPUT)
SPI_DDR &= ~_BV(DI_PIN); //pinMode(DI_PIN, INPUT)
#else
// Warning: if the SS pin ever becomes a LOW INPUT then SPI
// automatically switches to Slave, so the data direction of
// the SS pin MUST be kept as OUTPUT.
SPCR |= _BV(MSTR);
SPCR |= _BV(SPE);
digitalWrite(SPI_PIN, HIGH);
pinMode(SPI_PIN, OUTPUT);
pinMode(USCK_PIN, OUTPUT);
pinMode(DI_PIN, INPUT);
pinMode(DO_PIN, OUTPUT);
#endif
}
void SPIClass::end() {
#ifdef __ATTINY
SPI_DDR &= ~(_BV(SPI_PIN) | _BV(USCK_PIN) | _BV(DO_PIN)); //pinMode(USCK_PIN, SPI_PIN, DO_PIN, INPUT)
#else
SPCR &= ~_BV(SPE);
#endif
}
void SPIClass::setBitOrder(uint8_t bitOrder)
{
#ifndef __ATTINY
if(bitOrder == LSBFIRST) {
SPCR |= _BV(DORD);
} else {
SPCR &= ~(_BV(DORD));
}
#else
//TODO
#endif
}
void SPIClass::setDataMode(uint8_t mode)
{
#ifndef __ATTINY
SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
#endif
}
void SPIClass::setClockDivider(uint8_t rate)
{
#ifndef __ATTINY
SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
#endif
}