-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
572 lines (502 loc) · 12.5 KB
/
main.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "lpc51u68.hpp"
#include <cstdint>
#include <alloca.h>
#define NOP() __asm__("nop" : : :)
/* Red: P0_29
* Grn: P1_10
* Blu: P1_9
* */
/*
* Port 1 (0:16) can be used for the address line
* Port 1 (17) can be a strobe of some kind
* Port 0 will be the data line, although it'll be scrambled.
* Port 0 can also hold additional strobes.
*
* Strobes: OE, CE, PGM
* */
static constexpr int PinOE { 25 };
static constexpr int PinWR { 26 }; // shared with A18 for extended (4M) devices
static constexpr int PinCE { 32+17 };
static constexpr uint8_t ACK { 0 };
static void SetLed(uint8_t state)
{
if (state == 0)
GPIO::CLR0 = 1 << 29;
else
GPIO::SET0 = 1 << 29;
}
enum ProgrammingFlags : uint8_t
{
PF_Verify = 1,
PF_Retain = 2,
PF_Dump = 1 << 4,
PF_Extended = 1 << 5,
PF_ExtendedPage = 1 << 6
};
void SendToHost(uint8_t value);
void SendState(const char* message);
void SendStringToHost(const char* message);
void SendHexToHost(uint32_t value, int nDigits);
void BeginLatch(void);
void EraseAll(void);
void Wait(void);
uint8_t ReadByte(void);
void Program(void);
void QuickProgram(uint32_t address, uint8_t value, bool preserveSector);
bool Program(uint8_t verificationByte);
void Dump(void);
void BeginProgram(void);
using size_t = std::size_t;
static volatile uint8_t s_data[256];
static volatile uint32_t s_dataMask[256/32];
static volatile uint8_t s_rxLog[1024];
static volatile uint16_t s_iRxLog;
static volatile uint32_t s_startAddress;
static volatile uint16_t s_count;
static volatile uint8_t s_programmingFlags;
static volatile bool s_startProgramming;
static volatile uint8_t s_rxState = 0;
static volatile uint16_t s_rxCount = 0;
static volatile uint16_t s_rxIndex = 0;
size_t strlen(const char* str)
{
const char* p = str;
while(*p++);
return p-str;
}
static uint32_t s_address;
void SetAddress(uint32_t address)
{
s_address = address;
GPIO::MASK1 = ~0x1FFFF;
GPIO::MPIN1 = address;
}
void SendState(const char* message)
{
SendStringToHost(message);
SendToHost(':');
SendToHost(' ');
SendHexToHost(GPIO::PIN0.value(), 8);
SendToHost(' ');
SendHexToHost(GPIO::PIN1.value(), 5);
SendToHost('\n');
}
void LatchByte(uint32_t address, uint8_t value)
{
/*
* tWP: 90ns (WR# held low)
* tWPH: 100ns (Minimum time of WR# high after rising edge)
* tDS: 50ns (Data valid before rising edge of WR#)
* tDH: 0ns (Data valid after rising edge of WR#)
* tCH: 0ns (Time between rising edge of WR# and rising edge of CE#)
* */
GPIO::BYTE.Volatile()->PBYTE[PinOE] = 1;
SetAddress(address);
GPIO::DIRSET0 = (0x3F << 5) | (3 << 18);
GPIO::MASK0 = ~((0x3F << 5) | (3 << 18));
GPIO::MPIN0 = ((value & 0x3F) << 5) | ((value & 0xC0) << (18-6));
GPIO::BYTE.Volatile()->PBYTE[PinWR] = 0;
GPIO::BYTE.Volatile()->PBYTE[PinCE] = 0;
NOP(); NOP(); NOP(); // at 12 MHz, 1 instruction is 83ns
GPIO::BYTE.Volatile()->PBYTE[PinCE] = 1;
GPIO::BYTE.Volatile()->PBYTE[PinWR] = 1;
GPIO::DIRCLR0 = (0x3F << 5) | (3 << 18);
}
void BeginLatch(void)
{
GPIO::BYTE.Volatile()->PBYTE[PinWR] = 0;
}
void QuickProgram(uint32_t address, uint8_t value, bool preserveSector)
{
if (preserveSector)
{
uint8_t data[256];
uint32_t tempAddress = address & ~0xFF;
for (int i = 0; i < 256; ++i)
{
SetAddress(tempAddress++);
data[i] = ReadByte();
}
data[address & 0xFF] = value;
BeginProgram();
tempAddress = address & ~0xFF;
for (int i=0; i<256; ++i)
LatchByte(tempAddress, data[i]);
}
else
{
BeginProgram();
LatchByte(address, value);
}
}
void BeginProgram(void)
{
LatchByte(0x5555, 0xAA);
LatchByte(0x2AAA, 0x55);
LatchByte(0x5555, 0xA0);
for (volatile uint32_t timeout = 20; timeout != 0; --timeout) ;
}
void Program(void)
{
// literally just wait 150usec I guess
for (volatile uint32_t timeout = 500; timeout != 0; --timeout) ;
Wait();
}
bool Program(uint8_t verificationByte)
{
for (volatile uint32_t timeout = 1000; timeout != 0; --timeout) ;
for (int attempts = 50; attempts != 0; --attempts)
{
if (ReadByte() == verificationByte)
return true;
for (volatile uint32_t timeout = 60; timeout != 0; --timeout) ;
}
return false;
}
uint8_t ReadByte(void)
{
GPIO::BYTE.Volatile()->PBYTE[PinCE] = 0;
GPIO::BYTE.Volatile()->PBYTE[PinOE] = 0;
GPIO::DIRCLR0.Volatile() = (0x3F << 5) | (3 << 18);
uint8_t value = ((GPIO::PIN0.Volatile().value() >> 5) & 0x3F) | ((GPIO::PIN0.Volatile().value() >> (18-6)) & 0xC0);
GPIO::BYTE.Volatile()->PBYTE[PinOE] = 1;
GPIO::BYTE.Volatile()->PBYTE[PinCE] = 1;
return value;
}
void SendToHost(uint8_t value)
{
while (!USART0::FIFOSTAT.Volatile()->TXNOTFULL) ;
USART0::FIFOWR.Volatile() = value;
}
void SendStringToHost(const char* message)
{
size_t len = strlen(message);
for (size_t i = 0; i < len; ++i)
SendToHost(message[i]);
}
void SendHexToHost(uint32_t value, int nDigits)
{
for (int iDigit = 0; iDigit < nDigits; ++iDigit)
{
uint8_t digit = (value >> ((nDigits - iDigit - 1) * 4)) & 0xF;
SendToHost(digit <= 9 ? (digit + '0') : (digit - 0xA + 'A'));
}
}
int main(void)
{
//SYSCON::MAINCLKSELA = 3; // fro_hf 96MHz
//SYSCON::MAINCLKSELB = 0; // MAINCLKSELA
SYSCON::AHBCLKCTRLSET0 = decltype(*SYSCON::AHBCLKCTRLSET0) {
.IOCON = 1,
.GPIO0 = 1,
.GPIO1 = 1,
};
SYSCON::AHBCLKCTRLSET1 = decltype(*SYSCON::AHBCLKCTRLSET1) { .FLEXCOMM0 = 1 };
// 1. Set to FRO_12M
// 2. Call API to select 96MHz
// 3. Set HSPDCLK (FROCTRL.bit30)
// 4. Set to FRO_HF
SYSCON::FCLKSEL0 = 0; // fro_12m
s_iRxLog = 0;
s_startProgramming = 0;
s_programmingFlags = 0;
s_rxState = 0;
s_count = 0;
s_rxState = 0;
s_rxIndex = 0;
Flexcomm0::PSELID->PERSEL = 1; // USART
// BRGVAL = ((FCLK / (OSRVAL+1)) / baud) - 1
// L = ((12e6 / 13) / 38400) - 1 ≈ 24.03846154
// baud = (FCLK / (OSRVAL+1)) / (BRGVAL+1)
// = (12e6 / 13) / 24 ≈ 38461.53846
//USART0::BRG = 23; //38400 (38462, 0.16% error)
USART0::OSR = 7;
USART0::BRG = 12; //115200
// (12e6/8) / 115200 - 1 = 12.02083333
// (12e6/8)/13 = 115384.6154 (0.16% error)
USART0::CFG.Volatile()->ENABLE = 1;
USART0::CFG.Volatile()->DATALEN = 1;
USART0::FIFOCFG->ENABLETX = 1;
USART0::FIFOCFG->ENABLERX = 1;
IOCON::PORT0->Pins[0].FUNC = 1;
IOCON::PORT0->Pins[1].FUNC = 1;
// Set all data lines to use a pull-down instead of a pull-up
IOCON::PORT0->Pins[5].MODE = 1;
IOCON::PORT0->Pins[6].MODE = 1;
IOCON::PORT0->Pins[7].MODE = 1;
IOCON::PORT0->Pins[8].MODE = 1;
IOCON::PORT0->Pins[9].MODE = 1;
IOCON::PORT0->Pins[10].MODE = 1;
IOCON::PORT0->Pins[18].MODE = 1;
IOCON::PORT0->Pins[19].MODE = 1;
// Set all address lines to use GPIO
for (int n = 0; n <= 16; ++n)
{
IOCON::PORT1->Pins[n].FUNC = 0; // GPIO function
IOCON::PORT1->Pins[n].MODE = 1; // pull-down (default LOW)
}
GPIO::BYTE->PBYTE[PinOE] = 1;
GPIO::BYTE->PBYTE[PinCE] = 1;
GPIO::BYTE->PBYTE[PinWR] = 1;
(&GPIO::DIRSET0)[PinOE/32] = 1<<(PinOE%32);
(&GPIO::DIRSET0)[PinCE/32] = 1<<(PinCE%32);
(&GPIO::DIRSET0)[PinWR/32] = 1<<(PinWR%32);
GPIO::DIRSET1 = 0x1FFFF;
/* Red: P0_29
* Grn: P1_10
* Blu: P1_9
* */
GPIO::DIRSET0 = (1<<29);
GPIO::BYTE->PBYTE[29] = 0;
GPIO::BYTE->PBYTE[10+32] = 1;
GPIO::BYTE->PBYTE[32+9] = 0;
uint32_t testAddress = 1;
//EraseAll();
#if 1
// Enter Identification mode
LatchByte(0x5555, 0xAA);
LatchByte(0x2AAA, 0x55);
LatchByte(0x5555, 0x90);
for (volatile uint32_t timeout = 0x10000; timeout != 0; --timeout) ;
// wait 10ms
SetAddress(0);
uint8_t manufacturer = ReadByte();
//SendStringToHost("\nManufacturer ");
//SendHexToHost(manufacturer, 2);
//SendToHost('\n');
SetAddress(1);
uint8_t device = ReadByte();
//SendStringToHost("Product ");
//SendHexToHost(device, 2);
//SendToHost('\n');
SetAddress(2);
uint8_t protection = ReadByte();
//SendStringToHost("Protection ");
//SendHexToHost(protection, 2);
//SendToHost('\n');
// Leave Identification mode
LatchByte(0x5555, 0xAA);
LatchByte(0x2AAA, 0x55);
LatchByte(0x5555, 0xF0);
for (volatile uint32_t timeout = 0x10000; timeout != 0; --timeout) ;
// wait 10ms
SetAddress(0);
uint8_t test0 = ReadByte();
//SendStringToHost("@ 0: ");
//SendHexToHost(test0, 2);
//SendToHost('\n');
//SendToHost('\n');
#endif
USART0::FIFOTRIG.Volatile()->RXLVLENA = 1;
USART0::FIFOTRIG.Volatile()->RXLVL = 0,
USART0::FIFOINTENSET.Volatile()->RXLVL = 1;
NVIC_EnableIRQ(Flexcomm0_IRQn);
DoItAllAgain:
while (!s_startProgramming) ;
if ((s_programmingFlags & PF_Dump) == PF_Dump)
{
if (s_programmingFlags & PF_Extended)
GPIO::BYTE.Volatile()->PBYTE[PinWR] = (s_programmingFlags & PF_ExtendedPage) != 0;
Dump();
if (s_programmingFlags & PF_Extended)
GPIO::BYTE.Volatile()->PBYTE[PinWR] = 0;
s_startProgramming = false;
s_rxState = 0;
SendToHost(ACK);
goto DoItAllAgain;
}
if (s_programmingFlags & PF_Retain)
{
uint32_t sector = s_startAddress & ~0xFF;
bool isAlreadyUpToDate = true;
for (int i=0; i < s_count; ++i)
{
if ((s_dataMask[i>>5] & (1 << (i & 31))) == 0)
{
SetAddress(sector | i);
s_data[i] = ReadByte();
}
else if (isAlreadyUpToDate)
{
SetAddress(sector | i);
if (ReadByte() != s_data[i])
isAlreadyUpToDate = false;
}
}
if (isAlreadyUpToDate)
{
SendToHost(0);
s_rxState = 0;
s_startProgramming = false;
goto DoItAllAgain;
}
s_startAddress = sector;
s_count = 256;
}
uint8_t lastByte = 0;
for (int attempts = 10; attempts != 0; --attempts)
{
BeginProgram();
const uint32_t sector = 1;
for (int i=0; i<s_count; ++i)
{
uint32_t address = s_startAddress + i;
LatchByte(address, lastByte = s_data[address & 0xFF]);
}
if (Program(lastByte))
goto Ok;
}
s_rxState = 0;
s_startProgramming = false;
SendToHost(2);
SendToHost(lastByte);
SendToHost(ReadByte());
goto DoItAllAgain;
Ok:
if (s_programmingFlags & PF_Verify)
{
int nErrors = 0;
uint8_t* errorOffsets = (uint8_t*) alloca(2 * s_count);
for (int i=0; i<s_count; ++i)
{
uint32_t address = ((s_startAddress + i) & 0xFF) | (s_startAddress & ~0xFF);
SetAddress(address);
uint8_t newValue = ReadByte();
if (newValue != s_data[address & 0xFF])
{
errorOffsets[nErrors * 2] = i;
errorOffsets[nErrors * 2 + 1] = newValue;
++nErrors;
}
}
if (nErrors > 0)
{
SendToHost(1);
SendToHost(nErrors-1);
for (int i=0; i < nErrors * 2; ++i)
{
SendToHost(errorOffsets[i*2]);
SendToHost(errorOffsets[i*2+1]);
}
}
else
SendToHost(0);
}
else
SendToHost(0);
s_rxState = 0;
s_startProgramming = false;
goto DoItAllAgain;
return 0;
}
void Dump(void)
{
for (int i=0; i<s_count; ++i)
{
SetAddress(s_startAddress + i);
uint8_t value = ReadByte();
//SendHexToHost(s_address, 6);
//SendToHost(' ');
//SendToHost('=');
//SendToHost(' ');
//SendHexToHost(value, 2);
//SendToHost('\n');
SendToHost(value);
}
}
void EraseAll(void)
{
LatchByte(0x5555, 0xAA);
LatchByte(0x2AAA, 0x55);
LatchByte(0x5555, 0x80);
LatchByte(0x5555, 0xAA);
LatchByte(0x2AAA, 0x55);
LatchByte(0x5555, 0x10);
for (volatile uint32_t timeout = 0x40000; timeout != 0; --timeout) ;
Wait();
}
void Wait(void)
{
uint8_t value = ReadByte();
uint8_t value2 = ReadByte();
for (;;)
{
if ((value & (1<<6)) == (value2 & (1<<6)))
break;
}
}
static inline uint8_t ReadUart(void)
{
uint8_t value = USART0::FIFORD->RXDATA & 0xFF;
s_rxLog[s_iRxLog++] = value;
s_iRxLog &= 1023;
return value;
}
extern "C" void FLEXCOMM0_IRQHandler(void)
{
//if (USART0::FIFOSTAT->RXNOTEMPTY)
{
uint8_t data = ReadUart();
switch(s_rxState)
{
case 0: // initial state, next byte is address
{
s_startAddress = data << 24;
if (s_startAddress == 0xFF000000) // programming flags
s_rxState = 7;
else
++s_rxState;
} break;
case 1: // partial reception of address
case 2:
case 3:
{
s_startAddress |= data << (24 - (8 * s_rxState));
++s_rxState;
} break;
case 4: // count
{
s_count = data + 1;
if ((s_programmingFlags & PF_Dump) == PF_Dump)
{
s_rxState = 6;
s_startProgramming = true;
}
else
{
++s_rxState;
s_rxIndex = s_startAddress & 0xFF;
s_rxCount = s_count;
for (int i=0; i < (256/32); ++i)
s_dataMask[i] = 0;
}
} break;
case 5: // data
{
s_data[s_rxIndex] = data;
s_dataMask[s_rxIndex >> 5] |= 1 << (s_rxIndex & 31);
++s_rxIndex;
s_rxIndex &= 0xFF;
if ((--s_rxCount) == 0)
{
s_startProgramming = true;
++s_rxState;
}
} break;
case 7: // programming flags
{
// b0: Verify
// b1: Keep unspecified bytes
s_programmingFlags = data;
s_rxState = 0;
SetLed((s_programmingFlags & 0xF0) != 0);
} break;
default:
{
//USART0::FIFOCFG->EMPTYRX = 1;
//(void) data; // read and discard
//s_iRxLog = (s_iRxLog - 1) & 1023;
} break;
}
}
}