-
Notifications
You must be signed in to change notification settings - Fork 197
/
GxIO_SPI.cpp
213 lines (189 loc) · 4.17 KB
/
GxIO_SPI.cpp
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// created by Jean-Marc Zingg to be the GxIO_SPI io class for the GxEPD library
//
// License: GNU GENERAL PUBLIC LICENSE V3, see LICENSE
//
#include "GxIO_SPI.h"
#if defined(PARTICLE)
// transfer16 is not used by GxEPD
#define transfer16 transfer
#endif
GxIO_SPI::GxIO_SPI(SPIClass& spi, int8_t cs, int8_t dc, int8_t rst, int8_t bl) :
_spi(spi), _spi_settings(4000000, MSBFIRST, SPI_MODE0),
_cs(cs), _dc(dc), _rst(rst), _bl(bl)
{
}
void GxIO_SPI::reset()
{
if (_rst >= 0)
{
delay(20);
digitalWrite(_rst, LOW);
delay(20);
digitalWrite(_rst, HIGH);
delay(200);
}
}
void GxIO_SPI::init()
{
if (_cs >= 0)
{
digitalWrite(_cs, HIGH);
pinMode(_cs, OUTPUT);
}
if (_dc >= 0)
{
digitalWrite(_dc, HIGH);
pinMode(_dc, OUTPUT);
}
if (_rst >= 0)
{
digitalWrite(_rst, HIGH);
pinMode(_rst, OUTPUT);
}
if (_bl >= 0)
{
digitalWrite(_bl, HIGH);
pinMode(_bl, OUTPUT);
}
reset();
_spi.begin();
if (_dc == MISO) // may be overridden, TTGO T5 V2.66
{
pinMode(_dc, OUTPUT);
}
}
void GxIO_SPI::setFrequency(uint32_t freq)
{
_spi_settings = SPISettings(freq, MSBFIRST, SPI_MODE0);
}
uint8_t GxIO_SPI::transferTransaction(uint8_t d)
{
_spi.beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
uint8_t rv = _spi.transfer(d);
if (_cs >= 0) digitalWrite(_cs, HIGH);
_spi.endTransaction();
return rv;
}
uint16_t GxIO_SPI::transfer16Transaction(uint16_t d)
{
_spi.beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
uint16_t rv = _spi.transfer16(d);
if (_cs >= 0) digitalWrite(_cs, HIGH);
_spi.endTransaction();
return rv;
}
uint8_t GxIO_SPI::readDataTransaction()
{
_spi.beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
uint8_t rv = _spi.transfer(0xFF);
if (_cs >= 0) digitalWrite(_cs, HIGH);
_spi.endTransaction();
return rv;
}
uint16_t GxIO_SPI::readData16Transaction()
{
_spi.beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
uint16_t rv = _spi.transfer16(0xFFFF);
if (_cs >= 0) digitalWrite(_cs, HIGH);
_spi.endTransaction();
return rv;
}
uint8_t GxIO_SPI::readData()
{
uint8_t rv = _spi.transfer(0xFF);
return rv;
}
uint16_t GxIO_SPI::readData16()
{
uint16_t rv = _spi.transfer16(0xFFFF);
return rv;
}
void GxIO_SPI::writeCommandTransaction(uint8_t c)
{
_spi.beginTransaction(_spi_settings);
if (_dc >= 0) digitalWrite(_dc, LOW);
if (_cs >= 0) digitalWrite(_cs, LOW);
_spi.transfer(c);
if (_cs >= 0) digitalWrite(_cs, HIGH);
if (_dc >= 0) digitalWrite(_dc, HIGH);
_spi.endTransaction();
}
void GxIO_SPI::writeDataTransaction(uint8_t d)
{
_spi.beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
_spi.transfer(d);
if (_cs >= 0) digitalWrite(_cs, HIGH);
_spi.endTransaction();
}
void GxIO_SPI::writeData16Transaction(uint16_t d, uint32_t num)
{
_spi.beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
writeData16(d, num);
if (_cs >= 0) digitalWrite(_cs, HIGH);
_spi.endTransaction();
}
void GxIO_SPI::writeCommand(uint8_t c)
{
if (_dc >= 0) digitalWrite(_dc, LOW);
_spi.transfer(c);
if (_dc >= 0) digitalWrite(_dc, HIGH);
}
void GxIO_SPI::writeData(uint8_t d)
{
_spi.transfer(d);
}
void GxIO_SPI::writeData(uint8_t* d, uint32_t num)
{
#if defined(ESP8266) || defined(ESP32)
_spi.writeBytes(d, num);
#else
while (num > 0)
{
_spi.transfer(*d);
d++;
num--;
}
#endif
}
void GxIO_SPI::writeData16(uint16_t d, uint32_t num)
{
#if defined(ESP8266) || defined(ESP32)
uint8_t b[2] = {uint8_t(d >> 8), uint8_t(d)};
_spi.writePattern(b, 2, num);
#else
while (num > 0)
{
_spi.transfer16(d);
num--;
}
#endif
}
void GxIO_SPI::writeAddrMSBfirst(uint16_t d)
{
_spi.transfer(d >> 8);
_spi.transfer(d & 0xFF);
}
void GxIO_SPI::startTransaction()
{
_spi.beginTransaction(_spi_settings);
if (_cs >= 0) digitalWrite(_cs, LOW);
}
void GxIO_SPI::endTransaction()
{
if (_cs >= 0) digitalWrite(_cs, HIGH);
_spi.endTransaction();
}
void GxIO_SPI::selectRegister(bool rs_low)
{
if (_dc >= 0) digitalWrite(_dc, (rs_low ? LOW : HIGH));
}
void GxIO_SPI::setBackLight(bool lit)
{
if (_bl >= 0) digitalWrite(_bl, (lit ? HIGH : LOW));
}