-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwiring_gd.h
More file actions
298 lines (260 loc) · 5.53 KB
/
wiring_gd.h
File metadata and controls
298 lines (260 loc) · 5.53 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
// spi chip select arduino
//#define CS 8
// spi chip select modemcu with arduino ide
//https://github.com/esp8266/Arduino/issues/1429
#define CS 15
class GDTransport {
private:
byte model;
public:
void begin0() {
Serial.println("begin0");
pinMode(CS, OUTPUT);
digitalWrite(CS, HIGH);
SPI.begin();
SPI.beginTransaction (SPISettings (100000, MSBFIRST, SPI_MODE0));
SPI.setClockDivider(SPI_CLOCK_DIV32);
#ifdef TEENSYDUINO
SPI.beginTransaction(SPISettings(3000000, MSBFIRST, SPI_MODE0));
#else
#ifndef __DUE__
//SPI.setClockDivider(SPI_CLOCK_DIV2);
//SPSR = (1 << SPI2X);
#endif
#endif
hostcmd(0x00);
#if (BOARD != BOARD_GAMEDUINO23)
hostcmd(0x44); // from external crystal
#endif
hostcmd(0x68);
}
void begin1() {
Serial.println("begin1, waiting for FT81x to respond...");
byte stat = __rd16(0xc0000UL) & 0xff;
Serial.println(stat, HEX);
while ((__rd16(0xc0000UL) & 0xff) != 0x08) {
//yield(); // avoid 8266 reset while waiting...
};
stat = __rd16(0xc0000UL) & 0xff;
Serial.print("status:");
Serial.println(stat, HEX);
// So that FT800,801 FT81x
// model 0 1
ft8xx_model = __rd16(0x0c0000) >> 12;
Serial.print("modell:");
Serial.println(ft8xx_model, HEX);
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;
SPI.transfer(b[0]);
SPI.transfer(b[1]);
SPI.transfer(b[2]);
SPI.transfer(b[3]);
}
void cmdbyte(byte x) {
if (freespace == 0) {
getfree(1);
}
wp++;
freespace--;
SPI.transfer(x);
}
void cmd_n(byte *s, uint16_t n) {
if (freespace < n) {
getfree(n);
}
wp += n;
freespace -= n;
while (n > 8) {
n -= 8;
SPI.transfer(*s++);
SPI.transfer(*s++);
SPI.transfer(*s++);
SPI.transfer(*s++);
SPI.transfer(*s++);
SPI.transfer(*s++);
SPI.transfer(*s++);
SPI.transfer(*s++);
}
while (n--)
SPI.transfer(*s++);
}
void flush() {
getfree(0);
}
uint16_t rp() {
uint16_t r = __rd16(REG_CMD_READ);
if (r == 0xfff) {
GD.alert("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);
SPI.transfer(0); // dummy
byte r = SPI.transfer(0);
stream();
return r;
}
void wr(uint32_t addr, byte v)
{
__end(); // stop streaming
__wstart(addr);
SPI.transfer(v);
stream();
}
uint16_t rd16(uint32_t addr)
{
uint16_t r = 0;
__end(); // stop streaming
__start(addr);
SPI.transfer(0);
r = SPI.transfer(0);
r |= (SPI.transfer(0) << 8);
stream();
return r;
}
void wr16(uint32_t addr, uint32_t v)
{
__end(); // stop streaming
__wstart(addr);
SPI.transfer(v);
SPI.transfer(v >> 8);
stream();
}
uint32_t rd32(uint32_t addr)
{
__end(); // stop streaming
__start(addr);
SPI.transfer(0);
union {
uint32_t c;
uint8_t b[4];
};
b[0] = SPI.transfer(0);
b[1] = SPI.transfer(0);
b[2] = SPI.transfer(0);
b[3] = SPI.transfer(0);
stream();
return c;
}
void rd_n(byte *dst, uint32_t addr, uint16_t n)
{
__end(); // stop streaming
__start(addr);
SPI.transfer(0);
while (n--)
*dst++ = SPI.transfer(0);
stream();
}
void wr32(uint32_t addr, unsigned long v)
{
__end(); // stop streaming
__wstart(addr);
SPI.transfer(v);
SPI.transfer(v >> 8);
SPI.transfer(v >> 16);
SPI.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();
}
static void __start(uint32_t addr) // start an SPI transaction to addr
{
digitalWrite(CS, LOW);
SPI.transfer(addr >> 16);
SPI.transfer(highByte(addr));
SPI.transfer(lowByte(addr));
}
static void __wstart(uint32_t addr) // start an SPI write transaction to addr
{
digitalWrite(CS, LOW);
SPI.transfer(0x80 | (addr >> 16));
SPI.transfer(highByte(addr));
SPI.transfer(lowByte(addr));
}
static void __end() // end the SPI transaction
{
digitalWrite(CS, 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));
}
static unsigned int __rd16(uint32_t addr)
{
unsigned int r;
__start(addr);
SPI.transfer(0); // dummy
r = SPI.transfer(0);
r |= (SPI.transfer(0) << 8);
__end();
return r;
}
static void __wr16(uint32_t addr, unsigned int v)
{
__wstart(addr);
SPI.transfer(lowByte(v));
SPI.transfer(highByte(v));
__end();
}
static void hostcmd(byte a)
{
digitalWrite(CS, LOW);
SPI.transfer(a);
SPI.transfer(0x00);
SPI.transfer(0x00);
digitalWrite(CS, 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;
};