-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisp_serial_singleton.spin2
607 lines (391 loc) · 14.2 KB
/
isp_serial_singleton.spin2
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
'' =================================================================================================
''
'' File....... isp_serial_singleton.spin2
'' Purpose.... True mode, unbuffererd serial coms using smart pins
'' -- same features as original P1 FDS with minor enhancements/changes
'' -- VAR content moved to DAT so we could share this singleton object across all objects in demo
'' Authors.... Eric Smith, Chip Gracey, and Jon McPhalen
'' (singleton adaptation by Stephen M. Moraco from older file jm_serial.spin2)
'' -- see below for terms of use
'' E-mail..... stephen@ironsheep.biz
'' Started....
'' Updated.... 21 May 2021 by Stephen M. Moraco
''
'' =================================================================================================
{
Note: The dec(), bin(), and hex() methods will no longer require the digits parameter as
in older versions of FullDuplexSerial. Use fwdec(), fbin(), and fhex() for code that
requires a specific field width.
}
con { fixed io pins }
RX1 = 63 { I } ' programming / debug
TX1 = 62 { O }
SF_CS = 61 { O } ' serial flash
SF_SCK = 60 { O }
SF_SDO = 59 { O }
SF_SDI = 58 { I }
con { pst formatting }
HOME = 1
CRSR_XY = 2
CRSR_LF = 3
CRSR_RT = 4
CRSR_UP = 5
CRSR_DN = 6
BELL = 7
BKSP = 8
TAB = 9
LF = 10
CLR_EOL = 11
CLR_DN = 12
CR = 13
CRSR_X = 14
CRSR_Y = 15
CLS = 16
obj
nstr : "jm_nstrings" ' number-to-string
dat
rxp long 0
txp long 0
pbuf byte 0[80] ' padded strings
kLock byte 0
pub null()
'' This is not a top level object
pub start(baudrate)
'' Start simple serial coms on default pins at baudrate
startx(RX1, TX1, baudrate) ' use programming port
pub startx(rxpin, txpin, baud) | bitmode
'' Start simple serial coms on rxpin and txpin at baud
'longmove(@rxp, @rxpin, 2) ' save pins
rxp := rxpin
txp := txpin
bitmode := muldiv64(clkfreq, $1_0000, baud) & $FFFFFC00 ' set bit timing
bitmode |= 7 ' set bits (8)
org
fltl rxpin ' configure rx smart pin
wrpin ##P_ASYNC_RX, rxpin
wxpin bitmode, rxpin
drvl rxpin
fltl txpin ' configure tx smart pin
wrpin ##(P_ASYNC_TX | P_OE), txpin
wxpin bitmode, txpin
drvl txpin
end
pub rxflush()
'' Clear serial input
repeat
while (rxcheck() >= 0)
pub rxcheck() : rxbyte | check
'' Check for serial input
'' -- returns -1 if nothing available
rxbyte := -1
check := pinr(rxp)
if (check)
rxbyte := rdpin(rxp) >> 24
pub rxtime(ms) : b | mstix, t
'' Wait ms milliseconds for a byte to be received
'' -- returns -1 if no byte received, $00..$FF if byte
mstix := clkfreq / 1000
t := getct()
repeat
until ((b := rxcheck()) >= 0) or (((getct() - t) / mstix) > ms)
pub rx() : rxbyte
'' Wait for serial input
'' -- blocks!
repeat
rxbyte := rxcheck()
until (rxbyte >= 0)
pub tx(b)
'' Emit byte
wypin(txp, b)
txflush()
pub txn(b, n)
'' Emit byte n times
repeat n
tx(b)
pub txflush() | check
'' Wait until last byte has finished
repeat
check := pinr(txp)
while (check == 0)
pub str(p_str)
'' Emit z-string at p_str
repeat (strsize(p_str))
tx(byte[p_str++])
pub substr(p_str, len) | b
'' Emit len characters of string at p_str
'' -- aborts if end of string detected
repeat len
b := byte[p_str++]
if (b > 0)
tx(b)
else
quit
pub padstr(p_str, width, pad)
'' Emit p_str as padded field of width characters
'' -- pad is character to use to fill out field
'' -- positive width causes right alignment
'' -- negative width causes left alignment
str(nstr.padstr(p_str, width, pad))
con { formatted strings }
{{
Escaped characters
\\ backslash char
\% percent char
\q double quote
\b backspace
\t tab (horizontal)
\n new line (vertical tab)
\r carriage return
\nnn arbitrary ASCII value (nnn is decimal)
Formatted arguments
%w.pf print argument as decimal width decimal point
%[w[.p]]d print argument as decimal
%[w[.p]]x print argument as hex
%[w[.p]]o print argument as octal
%[w[.p]]q print argument as quarternary
%[w[.p]]b print argument as binary
%[w]s print argument as string
%[w]c print argument as character (
-- w is field width
* positive w causes right alignment in field
* negative w causes left alignment in field
-- %ws aligns s in field (may truncate)
-- %wc prints w copies of c
-- p is precision characters
* number of characters to use, aligned in field
-- prepends 0 if needed to match p
-- for %w.pf, p is number of digits after decimal point
}}
pub fstr0(p_str)
'' Emit string with formatting characters.
format(p_str, 0)
pub fstr1(p_str, arg1)
'' Emit string with formatting characters and one argument.
format(p_str, @arg1)
pub fstr2(p_str, arg1, arg2)
'' Emit string with formatting characters and two arguments.
format(p_str, @arg1)
pub fstr3(p_str, arg1, arg2, arg3)
'' Emit string with formatting characters and three arguments.
format(p_str, @arg1)
pub fstr4(p_str, arg1, arg2, arg3, arg4)
'' Emit string with formatting characters and four arguments.
format(p_str, @arg1)
pub fstr5(p_str, arg1, arg2, arg3, arg4, arg5)
'' Emit string with formatting characters and five arguments.
format(p_str, @arg1)
pub fstr6(p_str, arg1, arg2, arg3, arg4, arg5, arg6)
'' Emit string with formatting characters and six arguments.
format(p_str, @arg1)
pub fstr7(p_str, arg1, arg2, arg3, arg4, arg5, arg6, arg7)
'' Emit string with formatting characters and seven arguments.
format(p_str, @arg1)
pub format(p_str, p_args) | idx, c, asc, fld, digits, kindaLock
'' Emit formatted string with escape sequences and embedded values
'' -- p_str is a pointer to the format control string
'' -- p_args is pointer to array of longs that hold field values
'' * field values can be numbers, characters, or pointers to strings
repeat
kindaLock := kLock
kLock := 1
if kindaLock == 1
waitms(1)
while kindaLock == 1
idx := 0 ' value index
repeat
c := byte[p_str++]
if (c == 0)
return
elseif (c == "\")
c := byte[p_str++]
if (c == "\")
tx("\")
elseif (c == "%")
tx("%")
elseif (c == "q")
tx(34)
elseif (c == "b")
tx(BKSP)
elseif (c == "t")
tx(TAB)
elseif (c == "n")
tx(LF)
elseif (c == "r")
tx(CR)
elseif ((c >= "0") and (c <= "9"))
--p_str
p_str, asc, _ := get_nargs(p_str)
if ((asc >= 0) and (asc <= 255))
tx(asc)
elseif (c == "%")
p_str, fld, digits := get_nargs(p_str)
c := byte[p_str++]
if (c == "f")
str(nstr.fmt_number(long[p_args][idx++], 99, digits, fld, " "))
elseif (c == "d")
str(nstr.fmt_number(long[p_args][idx++], 10, digits, fld, " "))
elseif (c == "x")
str(nstr.fmt_number(long[p_args][idx++], 16, digits, fld, " "))
elseif (c == "o")
str(nstr.fmt_number(long[p_args][idx++], 08, digits, fld, " "))
elseif (c == "q")
str(nstr.fmt_number(long[p_args][idx++], 04, digits, fld, " "))
elseif (c == "b")
str(nstr.fmt_number(long[p_args][idx++], 02, digits, fld, " "))
elseif (c == "s")
str(nstr.padstr(long[p_args][idx++], fld, " "))
elseif (c == "c")
txn(long[p_args][idx++], (abs fld) #> 1)
else
tx(c)
kLock := 0 ' clear our lock
pri get_nargs(p_str) : p_str1, val1, val2 | c, sign
'' Parse one or two numbers from string in n, -n, n.n, or -n.n format
'' -- dpoint separates values
'' -- only first # may be negative
'' -- returns pointer to 1st char after value(s)
c := byte[p_str] ' check for negative on first value
if (c == "-")
sign := -1
++p_str
else
sign := 0
repeat ' get first value
c := byte[p_str++]
if ((c >= "0") and (c <= "9"))
val1 := (val1 * 10) + (c - "0")
else
if (sign)
val1 := -val1
quit
if (c == ".") ' if dpoint
repeat ' get second value
c := byte[p_str++]
if ((c >= "0") and (c <= "9"))
val2 := (val2 * 10) + (c - "0")
else
quit
p_str1 := p_str - 1 ' back up to non-digit
pub fmt_number(value, base, digits, width, pad)
'' Emit value converted to number in padded field
'' -- value is converted using base as radix
'' * 99 for decimal with digits after decimal point
'' -- digits is max number of digits to use
'' -- width is width of final field (max)
'' -- pad is character that fills out field
str(nstr.fmt_number(value, base, digits, width, pad))
pub dec(value)
'' Emit value as decimal
str(nstr.itoa(value, 10, 0))
pub fwdec(value, digits)
'' Emit value as decimal using fixed # of digits
'' -- may add leading zeros
str(nstr.itoa(value, 10, digits))
pub jdec(value, digits, width, pad)
'' Emit value as decimal using fixed # of digits
'' -- aligned in padded field (negative width to left-align)
'' -- digits is max number of digits to use
'' -- width is width of final field (max)
'' -- pad is character that fills out field
str(nstr.fmt_number(value, 10, digits, width, pad))
pub dpdec(value, dp)
'' Emit value as decimal with decimal point
'' -- dp is number of digits after decimal point
str(nstr.dpdec(value, dp))
pub jdpdec(value, dp, width, pad)
'' Emit value as decimal with decimal point
'' -- aligned in padded field (negative width to left-align)
'' -- dp is number of digits after decimal point
'' -- width is width of final field (max)
'' -- pad is character that fills out field
str(nstr.fmt_number(value, 99, dp, width, pad))
pub hex(value)
'' Emit value as hexadecimal
str(nstr.itoa(value, 16, 0))
pub fhex(value, digits)
'' Emit value as hexadecimal using fixed # of digits
str(nstr.itoa(value, 16, digits))
pub jhex(value, digits, width, pad)
'' Emit value as quarternary using fixed # of digits
'' -- aligned inside field
'' -- pad fills out field
str(nstr.fmt_number(value, 16, digits, width, pad))
pub oct(value)
'' Emit value as octal
str(nstr.itoa(value, 8, 0))
pub foct(value, digits)
'' Emit value as octal using fixed # of digits
str(nstr.itoa(value, 8, digits))
pub joct(value, digits, width, pad)
'' Emit value as octal using fixed # of digits
'' -- aligned inside field
'' -- pad fills out field
str(nstr.fmt_number(value, 8, digits, width, pad))
pub qrt(value)
'' Emit value as quarternary
str(nstr.itoa(value, 4, 0))
pub fqrt(value, digits)
'' Emit value as quarternary using fixed # of digits
str(nstr.itoa(value, 4, digits))
pub jqrt(value, digits, width, pad)
'' Emit value as quarternary using fixed # of digits
'' -- aligned inside field
'' -- pad fills out field
str(nstr.fmt_number(value, 4, digits, width, pad))
pub bin(value)
'' Emit value as binary
str(nstr.itoa(value, 2, 0))
pub fbin(value, digits)
'' Emit value as binary using fixed # of digits
str(nstr.itoa(value, 2, digits))
pub jbin(value, digits, width, pad)
'' Emit value as binary using fixed # of digits
'' -- aligned inside field
'' -- pad fills out field
str(nstr.fmt_number(value, 2, digits, width, pad))
pub memDump(pBytes, lenBytes, pMessage) | rowCount, rowLen, pCurrByte, lastRowByteCount, bytesSoFar
'' Dump rows of hex values with address preceeding
if pMessage
fstr1(string("** %s:\r\n"), pMessage)
rowCount := lenBytes / 16
lastRowByteCount := lenBytes - (rowCount * 16)
pCurrByte := pBytes
bytesSoFar := 0
' emit full lines
if rowCount > 0
repeat rowCount
memDumpRow(pCurrByte, 16)
pCurrByte += 16
bytesSoFar += 16
if bytesSoFar < lenBytes
' emit last line
memDumpRow(pCurrByte, lastRowByteCount)
pri memDumpRow(pBytes, lenBytes) | rowCount, rowLen, pCurrByte, bytIndex
' emit address
fstr1(string(" 0x%.8x: "), pBytes)
' emit 8 bytes
' emit gap
' emit 8 bytes
repeat bytIndex from 0 to lenBytes - 1
fstr1(string("0x%.2x "), BYTE [pBytes][bytIndex])
if bytIndex == 7
fstr0(string(" "))
fstr0(string("\r\n"))
con { license }
{{
Terms of Use: MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}}