-
Notifications
You must be signed in to change notification settings - Fork 29
/
iceprog.ino
406 lines (330 loc) · 7.88 KB
/
iceprog.ino
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* iceprog -- firmware scetch for Arduino based Lattice iCE programmers
*
* Chris B. <chris@protonic.co.uk> @ Olimex Ltd. <c> 2017
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Relevant Documents:
* -------------------
* http://www.latticesemi.com/~/media/Documents/UserManuals/EI/icestickusermanual.pdf
* http://www.micron.com/~/media/documents/products/data-sheet/nor-flash/serial-nor/n25q/n25q_32mb_3v_65nm.pdf
* http://www.ftdichip.com/Support/Documents/AppNotes/AN_108_Command_Processor_for_MPSSE_and_MCU_Host_Bus_Emulation_Modes.pdf
* https://www.olimex.com/Products/FPGA/iCE40/iCE40HX1K-EVB/
* https://github.com/Marzogh/SPIFlash
*/
#include <SPI.h>
// The demo uses SPIFlash v2.2.0, its files are included in the project folders the author is Prajwal Bhattaram - Marzogh
#include "SPIFlash.h"
#define LED 17
#define CDONE 3
#define RESET 2
#define UEXT_POWER 8
#define CS 13
//commands list
#define READ_ID 0x9F
#define PWR_UP 0xAB
#define PWR_DOWN 0xB9
#define WR_EN 0x06
#define BULK_ERASE 0xC7
#define SEC_ERASE 0xd8
#define PROG 0x02
#define READ 0x03
#define READ_ALL 0x83
#define CMD_ERROR 0xee
#define READY 0x44
#define EMPTY 0x45
#define FEND 0xc0
#define FESC 0xdb
#define TFEND 0xdc
#define TFESC 0xdd
#define cselect digitalWrite(CS,LOW)
#define deselect digitalWrite(CS,HIGH)
//Toggle iceprogduino with serial bridge by pressing the HWB button
#define HWB (PINE & B00000100)==0 // Check if the button has been pressed
#define HWB_INPUT DDRE &= B11111011 // Initialize the port
#define LED_G 7 // The GREEN LED is on Pin 7
#define LED_Y 9 // The YELLOW LED is on Pin 9
boolean isProg;
uint8_t rxframe[512], txframe[512], fcs,rfcs;
uint8_t membuf[256];
uint8_t data_buffer[256];
uint16_t txp;
uint32_t maxPage;
bool escaped;
SPIFlash flash(CS);
void secerase(uint32_t sector);
void decodeFrame(void);
void writepage(int pagenr);
bool readSerialFrame(void);
void flash_bulk_erase(void);
void SendID(void);
void sendframe();
void startframe(uint8_t command);
void addbyte(uint8_t newbyte);
void readAllPages(void);
void readpage(uint16_t adr);
void loopProg(void);
void loopBridge(void);
// FRAME: <FEND><CMD>{data if any}<FCS(sume of all bytes = 0xFF)><FEND>
// if FEND, FESC in data - <FEND>=<FESC><TFEND>; <FESC>=<FESC><TFESC>
void setup() {
// put your setup code here, to run once:
// Power Up UEXT
pinMode(CDONE,INPUT);
pinMode(RESET,OUTPUT);
pinMode(LED,OUTPUT);
digitalWrite(LED,0);
pinMode(CS,OUTPUT);
digitalWrite(CS,HIGH);
pinMode(UEXT_POWER, OUTPUT);
digitalWrite(UEXT_POWER, HIGH);
delay(1000);
digitalWrite(UEXT_POWER, LOW);
delay(500);
digitalWrite(RESET, HIGH);
Serial.begin(230400);
while (!Serial);
isProg=true;
HWB_INPUT; // Initialize HWB
Serial1.begin(115200);
pinMode(LED_Y,OUTPUT);
digitalWrite(LED_Y,isProg); //Yellow LED is Prog
pinMode(LED_G,OUTPUT);
digitalWrite(LED_G,!isProg); //Green LED is Bridge
}
void loop(){
if (isProg) loopProg();
else loopBridge();
if (HWB) {
isProg = !isProg;
digitalWrite(LED_Y,isProg);
digitalWrite(LED_G,!isProg);
delay(1000);
}
}
void loopBridge(){
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
}
void loopProg() {
if (readSerialFrame())
{
decodeFrame();
if (rfcs==0xff)
{
digitalWrite(RESET, LOW);
switch (rxframe[0]){
case FEND:
break;
case READ_ID:
SendID();
break;
case BULK_ERASE:
flash_bulk_erase();
break;
case SEC_ERASE:
flash.powerUp();
secerase((rxframe[1]<<8) | rxframe[2]);
flash.powerDown();
break;
case READ:
flash.powerUp();
readpage((rxframe[1]<<8) | rxframe[2]);
flash.powerDown();
break;
case READ_ALL:
readAllPages();
break;
case PROG:
writepage((rxframe[1]<<8) | rxframe[2]);
break;
default:
break;
}//switch
digitalWrite(RESET, HIGH);
}
}
}
void secerase(uint32_t sector){
flash.eraseBlock64K(sector<<8);
startframe(READY);
sendframe();
}
void decodeFrame(void){
int x,y;
escaped = false;
y = 1;
rfcs = rxframe[1];
rxframe[0]=rxframe[1];
for (x=2;x<512;x++)
{
switch (rxframe[x]){
case FEND:
x = 513;
break;
case FESC:
escaped = true;
break;
case TFEND:
if (escaped)
{
rxframe[y++] = FEND;
rfcs+=FEND;
escaped = false;
}
else
{
rxframe[y++]=TFEND;
rfcs+=TFEND;
}
break;
case TFESC:
if (escaped)
{
rxframe[y++]=FESC;
rfcs+=FESC;
escaped = false;
}
else
{
rxframe[y++]=TFESC;
rfcs+=TFESC;
}
break;
default:
escaped = false;
rxframe[y++] = rxframe[x];
rfcs+=rxframe[x];
break;
}
}
}
void writepage(int pagenr){
int x;
flash.powerUp();
for (x=0; x<256;x++)
membuf[x]=rxframe[x+3];
flash.writePage(pagenr,membuf);
flash.readPage(pagenr,data_buffer);
flash.powerDown();
for (int a = 0; a < 256; a++)
if (data_buffer[a] != membuf[a])
return;
startframe(READY);
sendframe();
}
//Reads a frame from Serial
bool readSerialFrame(void)
{
Serial.setTimeout(50);
if (!Serial)
Serial.begin(230400);
// Serial.begin(1000000);
while (Serial.available()) {
Serial.readBytesUntil(FEND,rxframe,512);
//if (rxframe[0]!=FEND)
return true;
}
return false;
}
void flash_bulk_erase(void)
{
flash.powerUp();
flash.eraseChip();
flash.powerDown();
startframe(READY);
addbyte(BULK_ERASE);
sendframe();
}
void SendID(void)
{
flash.powerUp();
uint32_t JEDEC = flash.getJEDECID();
flash.powerDown();
startframe(READ_ID);
addbyte(JEDEC >> 16);
addbyte(JEDEC >> 8);
addbyte(JEDEC >> 0);
sendframe();
}
void sendframe(){
fcs = 0xff - fcs;
addbyte(fcs);
txframe[txp++] = FEND;
Serial.write(txframe,txp);
}
void startframe(uint8_t command){
txframe[0]=FEND;
txframe[1]=command;
txp = 2;
fcs = command;
}
void addbyte(uint8_t newbyte)
{
fcs+=newbyte;
if (newbyte == FEND)
{
txframe[txp++] = FESC;
txframe[txp++] = TFEND;
} else
if (newbyte == FESC)
{
txframe[txp++] = FESC;
txframe[txp++] = TFESC;
} else
txframe[txp++] = newbyte;
}
void readAllPages(void){
flash.powerUp();
maxPage=0x2000;
delay(10);
int p;
for (p=0;p<maxPage;p++)
readpage(p);
startframe(READY);
sendframe();
//
flash.powerDown();
}
void readpage(uint16_t adr){
bool sendempty = true;
//delay(5);
flash.readPage(adr,data_buffer);
for (int a = 0; a < 256; a++){
if (data_buffer[a] != 0xff){
startframe(READ);
addbyte(adr >> 8);
addbyte(adr >> 0);
for (int b = 0;b<256;b++)
addbyte(data_buffer[b]);
sendframe();
sendempty=false;
break;
}
}
if (sendempty){
startframe(EMPTY);
addbyte(adr >> 8);
addbyte(adr >> 0);
sendframe();
}
}