/
SimpleSpiInterface.h
125 lines (104 loc) · 3.61 KB
/
SimpleSpiInterface.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
MIT License
Copyright (c) 2021 Brian T. Park
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef ACE_SPI_SIMPLE_SPI_INTERFACE_H
#define ACE_SPI_SIMPLE_SPI_INTERFACE_H
#include <stdint.h>
#include <Arduino.h>
namespace ace_spi {
/** Software SPI using shiftOut(). */
class SimpleSpiInterface {
public:
/**
* Constructor.
*
* @param latchPin the latch pin (CS)
* @param dataPin the data pin (MOSI)
* @param clockPin the clock pin (CLK)
*/
explicit SimpleSpiInterface(
uint8_t latchPin,
uint8_t dataPin,
uint8_t clockPin
) :
mLatchPin(latchPin),
mDataPin(dataPin),
mClockPin(clockPin)
{}
/** Initialize the various pins. */
void begin() const {
pinMode(mLatchPin, OUTPUT);
pinMode(mDataPin, OUTPUT);
pinMode(mClockPin, OUTPUT);
}
/** Reset the various pins. */
void end() const {
pinMode(mLatchPin, INPUT);
pinMode(mDataPin, INPUT);
pinMode(mClockPin, INPUT);
}
/** Begin SPI transaction. Pull latch LOW. */
void beginTransaction() const {
digitalWrite(mLatchPin, LOW);
}
/** End SPI transaction. Pull latch HIGH. */
void endTransaction() const {
digitalWrite(mLatchPin, HIGH);
}
/** Transfer 8 bits. */
void transfer(uint8_t value) const {
shiftOut(mDataPin, mClockPin, MSBFIRST, value);
}
/** Transfer 16 bits. */
void transfer16(uint16_t value) const {
uint8_t msb = (value & 0xff00) >> 8;
uint8_t lsb = (value & 0xff);
shiftOut(mDataPin, mClockPin, MSBFIRST, msb);
shiftOut(mDataPin, mClockPin, MSBFIRST, lsb);
}
/** Convenience method to send 8 bits a single transaction. */
void send8(uint8_t value) const {
beginTransaction();
transfer(value);
endTransaction();
}
/** Convenience method to send 16 bits a single transaction. */
void send16(uint16_t value) const {
beginTransaction();
transfer16(value);
endTransaction();
}
/** Convenience method to send 16 bits a single transaction. */
void send16(uint8_t msb, uint8_t lsb) const {
beginTransaction();
shiftOut(mDataPin, mClockPin, MSBFIRST, msb);
shiftOut(mDataPin, mClockPin, MSBFIRST, lsb);
endTransaction();
}
// Use default copy constructor. Delete the assignment operator which cannot
// be used with constant member variables.
SimpleSpiInterface(const SimpleSpiInterface&) = default;
SimpleSpiInterface& operator=(const SimpleSpiInterface&) = delete;
private:
uint8_t const mLatchPin;
uint8_t const mDataPin;
uint8_t const mClockPin;
};
} // ace_spi
#endif