-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEVETR.h
More file actions
executable file
·376 lines (336 loc) · 7.6 KB
/
EVETR.h
File metadata and controls
executable file
·376 lines (336 loc) · 7.6 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* EVETR.h
Copyright (C) 2017 George T. Gougoudis<george.gougoudis@gmail.com>
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/>.
*/
#ifndef __EVETR_H__
#define __EVETR_H__
#ifndef CS
#define CS 10
#endif
class EveTransport {
private:
byte CSpin;
byte model;
SPIClass* mSPI;
public:
EveTransport(uint8_t CSpin, SPIClass* mSPI){
this->CSpin = CSpin;
this->mSPI = mSPI;
}
void ios() {
pinMode(CSpin, OUTPUT);
digitalWrite(CSpin, HIGH);
//pinMode(5, OUTPUT);
//digitalWrite(5, HIGH);
mSPI->begin();
// for (;;) mSPI->transfer(0x33);
}
void begin0() {
ios();
mSPI->begin();
#ifdef TEENSYDUINO
mSPI->beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE0));
#else
#ifndef __DUE__
mSPI->setClockDivider(SPI_CLOCK_DIV2);
SPSR = (1 << SPI2X);
#endif
#endif
hostcmd(0x00);
#if (BOARD != BOARD_FTDI_81X)
hostcmd(0x44); // from external crystal
#endif
hostcmd(0x68);
}
void begin1() {
#if 0
delay(120);
#else
while ((__rd16(0xc0000UL) & 0xff) != 0x08)
;
#endif
// Test point: saturate SPI
while (0) {
digitalWrite(CSpin, LOW);
mSPI->transfer(0x55);
digitalWrite(CSpin, HIGH);
}
#if 0
// Test point: attempt to wake up FT8xx every 2 seconds
while (0) {
hostcmd(0x00);
delay(120);
hostcmd(0x68);
delay(120);
digitalWrite(CSpin, LOW);
Serial.println(mSPI->transfer(0x10), HEX);
Serial.println(mSPI->transfer(0x24), HEX);
Serial.println(mSPI->transfer(0x00), HEX);
Serial.println(mSPI->transfer(0xff), HEX);
Serial.println(mSPI->transfer(0x00), HEX);
Serial.println(mSPI->transfer(0x00), HEX);
Serial.println();
digitalWrite(CSpin, HIGH);
delay(2000);
}
#endif
// So that FT800,801 FT81x
// model 0 1
ft8xx_model = __rd16(0x0c0000) >> 12;
wp = 0;
freespace = 4096 - 4;
stream();
}
void cmd32(uint32_t x) {
if (freespace < 4) {
getfree(4);
}
wp += 4;
freespace -= 4;
union {
uint32_t c;
uint8_t b[4];
};
c = x;
mSPI->transfer(b[0]);
mSPI->transfer(b[1]);
mSPI->transfer(b[2]);
mSPI->transfer(b[3]);
}
void cmdbyte(byte x) {
if (freespace == 0) {
getfree(1);
}
wp++;
freespace--;
mSPI->transfer(x);
}
void cmd_n(byte *s, uint16_t n) {
if (freespace < n) {
getfree(n);
}
wp += n;
freespace -= n;
while (n > 8) {
n -= 8;
mSPI->transfer(*s++);
mSPI->transfer(*s++);
mSPI->transfer(*s++);
mSPI->transfer(*s++);
mSPI->transfer(*s++);
mSPI->transfer(*s++);
mSPI->transfer(*s++);
mSPI->transfer(*s++);
}
while (n--)
mSPI->transfer(*s++);
}
void flush() {
getfree(0);
}
uint16_t rp() {
uint16_t r = __rd16(REG_CMD_READ);
if (r == 0xfff) {
//this->eve->alert("COPROCESSOR EXCEPTION");
Serial.println("COPROCESSOR EXCEPTION");
}
return r;
}
void finish() {
wp &= 0xffc;
__end();
__wr16(REG_CMD_WRITE, wp);
while (rp() != wp)
;
stream();
}
byte rd(uint32_t addr)
{
__end(); // stop streaming
__start(addr);
mSPI->transfer(0); // dummy
byte r = mSPI->transfer(0);
stream();
return r;
}
void wr(uint32_t addr, byte v)
{
__end(); // stop streaming
__wstart(addr);
mSPI->transfer(v);
stream();
}
uint16_t rd16(uint32_t addr)
{
uint16_t r = 0;
__end(); // stop streaming
__start(addr);
mSPI->transfer(0);
r = mSPI->transfer(0);
r |= (mSPI->transfer(0) << 8);
stream();
return r;
}
void wr16(uint32_t addr, uint32_t v)
{
__end(); // stop streaming
__wstart(addr);
mSPI->transfer(v);
mSPI->transfer(v >> 8);
stream();
}
uint32_t rd32(uint32_t addr)
{
__end(); // stop streaming
__start(addr);
mSPI->transfer(0);
union {
uint32_t c;
uint8_t b[4];
};
b[0] = mSPI->transfer(0);
b[1] = mSPI->transfer(0);
b[2] = mSPI->transfer(0);
b[3] = mSPI->transfer(0);
stream();
return c;
}
void rd_n(byte *dst, uint32_t addr, uint16_t n)
{
__end(); // stop streaming
__start(addr);
mSPI->transfer(0);
while (n--)
*dst++ = mSPI->transfer(0);
stream();
}
#ifdef ARDUINO_AVR_UNO
void wr_n(uint32_t addr, byte *src, uint16_t n)
{
__end(); // stop streaming
__wstart(addr);
while (n--) {
SPDR = *src++;
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
asm volatile("nop");
}
while (!(SPSR & _BV(SPIF))) ;
stream();
}
#else
void wr_n(uint32_t addr, byte *src, uint16_t n)
{
__end(); // stop streaming
__wstart(addr);
while (n--)
mSPI->transfer(*src++);
stream();
}
#endif
void wr32(uint32_t addr, unsigned long v)
{
__end(); // stop streaming
__wstart(addr);
mSPI->transfer(v);
mSPI->transfer(v >> 8);
mSPI->transfer(v >> 16);
mSPI->transfer(v >> 24);
stream();
}
uint32_t getwp(void) {
return RAM_CMD + (wp & 0xffc);
}
void bulk(uint32_t addr) {
__end(); // stop streaming
__start(addr);
}
void resume(void) {
stream();
}
void __start(uint32_t addr) // start an SPI transaction to addr
{
digitalWrite(CSpin, LOW);
mSPI->transfer(addr >> 16);
mSPI->transfer(highByte(addr));
mSPI->transfer(lowByte(addr));
}
void __wstart(uint32_t addr) // start an SPI write transaction to addr
{
digitalWrite(CSpin, LOW);
mSPI->transfer(0x80 | (addr >> 16));
mSPI->transfer(highByte(addr));
mSPI->transfer(lowByte(addr));
}
void __end() // end the SPI transaction
{
digitalWrite(CSpin, HIGH);
}
void stop() // end the SPI transaction
{
wp &= 0xffc;
__end();
__wr16(REG_CMD_WRITE, wp);
// while (__rd16(REG_CMD_READ) != wp) ;
}
void stream(void) {
__end();
__wstart(RAM_CMD + (wp & 0xfff));
}
unsigned int __rd16(uint32_t addr)
{
unsigned int r;
__start(addr);
mSPI->transfer(0); // dummy
r = mSPI->transfer(0);
r |= (mSPI->transfer(0) << 8);
__end();
return r;
}
void __wr16(uint32_t addr, unsigned int v)
{
__wstart(addr);
mSPI->transfer(lowByte(v));
mSPI->transfer(highByte(v));
__end();
}
void hostcmd(byte a)
{
digitalWrite(CSpin, LOW);
mSPI->transfer(a);
mSPI->transfer(0x00);
mSPI->transfer(0x00);
digitalWrite(CSpin, HIGH);
}
void getfree(uint16_t n)
{
wp &= 0xfff;
__end();
__wr16(REG_CMD_WRITE, wp & 0xffc);
do {
uint16_t fullness = (wp - rp()) & 4095;
freespace = (4096 - 4) - fullness;
} while (freespace < n);
stream();
}
byte streaming;
uint16_t wp;
uint16_t freespace;
};
#endif