forked from benlaurie/arduino--
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi.h
91 lines (75 loc) · 2.08 KB
/
spi.h
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
88
89
90
91
#ifndef SPI_H
#define SPI_H
#include "arduino--.h"
class NullPin
{
public:
static void modeOutput() { }
static void modeInput() { }
static void set() { }
static void clear() { }
// Using read() or toggle() will trigger a compilation error.
};
template <class Sck, class Miso, class Mosi, class Ss=NullPin>
class _SPI
{
public:
static void wait()
{
while(!(SPSR & (1 << SPIF)))
;
}
// Default is MSB first, SPI mode 0, FOSC/4
static void init(byte config = 0, bool double_speed = false)
{
// initialize the SPI pins
Sck::modeOutput();
Mosi::modeOutput();
Miso::modeInput();
Ss::modeOutput();
Ss::set();
// FIXME: other SPI inits clear MOSI and SCK here. Needed?
mode(config, double_speed);
}
static void mode(byte config, bool double_speed)
{
byte tmp __attribute__((unused));
// enable SPI master with configuration byte specified
Register::SPCR = 0;
Register::SPCR = (config & 0x7F) | (1 << SPE) | (1 << MSTR);
// clear any pending conditions and set double speed if needed.
if (double_speed)
SPSR |= (1 << SPI2X);
else
tmp = SPSR;
tmp = SPDR;
}
static byte transfer(byte value, byte delay = 0)
{
Ss::clear();
SPDR = value;
wait();
Ss::set();
if (delay > 0)
::delayMicroseconds(delay);
return SPDR;
}
static uint16_t transfer(uint16_t value, byte delay = 0)
{
uint16_t result = 0;
Ss::clear();
SPDR = (value & 0xff00) >> 8;
wait();
result = SPDR << 8;
SPDR = value & 0xff;
wait();
result |= SPDR;
Ss::set();
if (delay > 0)
::delayMicroseconds(delay);
return result;
}
};
typedef _SPI<Pin::SPI_SCK, Pin::SPI_MISO, Pin::SPI_MOSI, NullPin> SPI;
typedef _SPI<Pin::SPI_SCK, Pin::SPI_MISO, Pin::SPI_MOSI, Pin::SPI_SS> SPISS;
#endif