-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiram.cpp
More file actions
333 lines (300 loc) · 7.43 KB
/
spiram.cpp
File metadata and controls
333 lines (300 loc) · 7.43 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
spiram.cpp - SPI RAM management
Copyright (C) 2018 Costin STROIE <costinstroie@eridu.eu.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "spiram.h"
SPIRAM::SPIRAM(int CS, uint16_t bufSize): cs(CS), bufSize(bufSize) {
// Initialize the RAM chip
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH);
delay(50);
digitalWrite(cs, LOW);
delay(50);
digitalWrite(cs, HIGH);
// Allocate one more byte (to make room for 16-bit operations)
buf = (uint8_t*)malloc(bufSize + 1);
}
SPIRAM::~SPIRAM() {
free(buf);
}
void SPIRAM::init() {
begin();
SPI.transfer(CMD_WRMR);
SPI.transfer(MODE_SEQ);
end();
}
void SPIRAM::clear() {
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_WRITE);
// Address
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0x00);
// Data
for (int x = 0x00; x <= 0xFF; ++x) {
for (int y = 0x00; y <= 0xFF; ++y)
SPI.transfer(0x00);
yield();
}
// End SPI transfer
end();
}
void SPIRAM::reset() {
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_RSTIO);
// End SPI transfer
end();
}
// Check if the address is contained in buffer
bool SPIRAM::inBuffer(uint16_t addr) {
return (addr >= bufStart and addr <= bufEnd);
}
// Flush the buffer, if dirty, and reset it
void SPIRAM::flush() {
// Write back the buffer into RAM
wrBuffer();
// Reset the start and end addresses
bufStart = LASTBYTE;
bufEnd = LASTBYTE;
}
// Flush the buffer, if dirty and the address is contained, and reset it
void SPIRAM::flush(uint16_t addr) {
// Check if the address is contained in buffer
if (inBuffer(addr))
// Need to flush
flush();
}
void SPIRAM::chBuffer(uint16_t addr) {
// Check if the address is contained in buffer
if (inBuffer(addr))
return;
// Write back the buffer into RAM
wrBuffer();
// Set new start address
if (addr > (LASTBYTE - bufSize))
bufStart = LASTBYTE - bufSize + 1;
else
bufStart = addr;
// End address
bufEnd = bufStart + bufSize - 1;
// Fetch the buffer from RAM
rdBuffer();
}
// Read a buffer from RAM and mark it clean
void SPIRAM::rdBuffer() {
// Read RAM data into buffer
read(bufStart, buf, bufSize + 1);
// Make it clean
bufDirty = false;
}
// Write a buffer to RAM, if dirty, and mark it clean
void SPIRAM::wrBuffer() {
if (bufDirty) {
// Write buffer data into RAM
write(bufStart, buf, bufSize + 1);
// Make it clean
bufDirty = false;
}
}
uint8_t SPIRAM::getByte(uint16_t addr) {
// Change the buffer
chBuffer(addr);
// Directly return the byte from the buffer
return buf[addr - bufStart];
}
void SPIRAM::setByte(uint16_t addr, uint8_t data) {
// Change the buffer
chBuffer(addr);
// Directly set the byte into the buffer
buf[addr - bufStart] = data;
// Mark it dirty
bufDirty = true;
}
uint16_t SPIRAM::getWord(uint16_t addr) {
// Change the buffer
chBuffer(addr);
// Directly return the byte from the buffer
uint16_t bufPos = addr - bufStart;
return buf[bufPos] + buf[bufPos + 1] * 0x0100;
}
void SPIRAM::setWord(uint16_t addr, uint16_t data) {
// Change the buffer
chBuffer(addr);
// Directly set the byte into the buffer
uint16_t bufPos = addr - bufStart;
buf[bufPos] = lowByte(data);
buf[bufPos + 1] = highByte(data);
bufDirty = true;
}
uint8_t SPIRAM::readByte(uint16_t addr) {
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_READ);
// Address
SPI.transfer(0x00);
SPI.transfer(highByte(addr));
SPI.transfer(lowByte(addr));
// Data
uint8_t result = SPI.transfer(0x00);
// End SPI transfer
end();
return result;
}
void SPIRAM::writeByte(uint16_t addr, uint8_t data) {
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_WRITE);
// Address
SPI.transfer(0x00);
SPI.transfer(highByte(addr));
SPI.transfer(lowByte(addr));
// Data
SPI.transfer(data);
// End SPI transfer
end();
}
uint16_t SPIRAM::readWord(uint16_t addr) {
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_READ);
// Address
SPI.transfer(0x00);
SPI.transfer(highByte(addr));
SPI.transfer(lowByte(addr));
// Data
uint16_t result = SPI.transfer(0x00) | (SPI.transfer(0x00) << 8);
// End SPI transfer
end();
return result;
}
void SPIRAM::writeWord(uint16_t addr, uint16_t data) {
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_WRITE);
// Address
SPI.transfer(0x00);
SPI.transfer(highByte(addr));
SPI.transfer(lowByte(addr));
// Data
SPI.transfer(lowByte(data));
SPI.transfer(highByte(data));
// End SPI transfer
end();
}
void SPIRAM::read(uint16_t addr, uint8_t *buf, uint16_t len) {
uint16_t i = 0;
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_READ);
// Address
SPI.transfer(0x00);
SPI.transfer(highByte(addr));
SPI.transfer(lowByte(addr));
// Data
while (len--)
buf[i++] = SPI.transfer(0x00);
// End SPI transfer
end();
}
void SPIRAM::write(uint16_t addr, uint8_t *buf, uint16_t len) {
uint16_t i = 0;
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_WRITE);
// Address
SPI.transfer(0x00);
SPI.transfer(highByte(addr));
SPI.transfer(lowByte(addr));
// Data
while (len--)
SPI.transfer(buf[i++]);
// End SPI transfer
end();
}
void SPIRAM::hexdump(uint16_t start, uint16_t stop, char* comment) {
char buf[16];
char val[4];
uint8_t data;
// Adjust start and stop addresses
start &= 0xFFF0;
stop |= 0x000F;
// Flush the buffer
flush();
// Start with a new line
Serial.print("\r\n");
// Print the comment
if (comment[0]) {
Serial.print(F("; "));
Serial.print(comment);
Serial.print(F("\r\n"));
}
// Begin SPI transfer
begin();
// Command
SPI.transfer(CMD_READ);
// Address
SPI.transfer(0x00);
SPI.transfer(highByte(start));
SPI.transfer(lowByte(start));
// All bytes
for (uint16_t addr = start; addr <= stop;) {
yield();
// Use the buffer to display the address
sprintf_P(buf, PSTR("%04X: "), addr);
Serial.print(buf);
// Iterate over bytes, 2 sets of 8 bytes
for (uint8_t set = 0; set < 2; set++) {
for (uint8_t byt = 0; byt < 8; byt++) {
// Read data
data = SPI.transfer(0x00);
// Prepare and print the hex dump
sprintf_P(val, PSTR("%02X "), data);
Serial.print(val);
// Prepare the ASCII dump
if (data < 0x20 or data > 0x7F)
data = '.';
buf[addr & 0x0F] = data;
// Increment the address
addr++;
}
// Print a separator
Serial.write(' ');
}
// Print the ASCII column
Serial.write('|');
for (uint8_t idx = 0; idx < 0x10; idx++)
Serial.write(buf[idx]);
// New line
Serial.print(F("|\r\n"));
}
// End SPI transfer
end();
}
void SPIRAM::begin() {
SPI.beginTransaction(SPISettings(SPI_SPEED, MSBFIRST, SPI_MODE0));
digitalWrite(cs, LOW);
}
void SPIRAM::end() {
digitalWrite(cs, HIGH);
SPI.endTransaction();
}