-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
isp_serial.spin2
592 lines (380 loc) · 13.3 KB
/
isp_serial.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
'' =================================================================================================
''
'' File....... isp_serial.spin2
'' Purpose.... True mode, unbuffererd serial coms using smart pins
'' -- same features as original jm_serial.spin2 by Jon McPhalen with minor enhancements
'' Authors.... Eric Smith, Chip Gracey, and Jon McPhalen
'' -- formatted strings inspired by clib.spin for P1 (author unknown)
'' -- see below for terms of use
'' E-mail..... [email protected]
'' Started.... Feb 2022
'' Updated.... 4 Feb 2022
''
'' =================================================================================================
'' stop bit support added by Stephen M Moraco 13 Jan 2022
CON { fixed io pins }
RX1 = 63
TX1 = 62
CON { smart pin modes }
TX_MODE = %0000_0000_000_0000000000000_01_11110_0
RX_MODE = %0000_0000_000_0000000000000_00_11111_0
CON { serial configuration }
DATA_BITS = 8
STOP_BITS = 2
' (following ignored for now)
#0, PARITY_NONE, PARITY_EVEN, PARITY_ODD
' tx pin pull-up constants
#0, PU_NONE, PU_1K5, PU_3K3, PU_15K ' pull-up options
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"
var
long rxp
long txp
long baud
long byteMask
long byteExtraBits
byte pbuf[80] ' padded strings
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, P_HIGH_15K)
PUB startx(rxpin, txpin, baudrate, txPullup) | bitperiod, bitmode, databits
'' Start simple serial coms on rxpin and txpin at baudrate
longmove(@rxp, @rxpin, 3) ' also save baudrate incase driver is asked
bitperiod := clkfreq / baudrate
case txPullup
PU_NONE : txPullup := P_HIGH_FLOAT ' use external pull-up
PU_1K5 : txPullup := P_HIGH_1K5 ' 1.5k
PU_3K3 : txPullup := P_HIGH_1MA ' acts like ~3.3k
other : txPullup := P_HIGH_15K ' 15K
databits := DATA_BITS
if (STOP_BITS == 2)
databits += 1
bitmode := 7 + (bitperiod << 16)
' setup constants for tx() to use
byteMask := $FF
if (DATA_BITS == 7)
byteMask := $7f
byteExtraBits := 0
if (STOP_BITS == 2)
byteExtraBits |= $100
pinf(txp)
wrpin(txp, TX_MODE | txPullup)
wxpin(txp, bitmode)
pinl(txp)
pinf(rxp)
wrpin(rxp, RX_MODE)
wxpin(rxp, bitmode)
pinl(rxp)
PUB stop()
pinclear(txp)
pinclear(rxp)
pinf(txp)
pinf(rxp)
PUB getBaudrate() : baudrate
'' Return the configured baud rate
baudrate := baud '' was saved by startx()
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
rxbyte &= byteMask ' handle 7bit data (e.g., 7N1 vs. 8N1)
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) | byt, mask
'' Emit byte
b &= byteMask
b |= byteExtraBits
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 }
{{
Escape sequences
\\ backslash char
\% percent char
\r carriage return
\n new line (vertical tab)
\t tab (horizontal)
\q double quote
\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 n causes right alignment in field
* negative n causes left align ment in field
-- %ns aligns s in field (may truncate)
-- %nc prints n copies of c
-- p is precision characters
* number of characters to use, aligned in field
-- for %n.pf, p is number of digits after dpoint
}}
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 format(p_str, p_args) | idx, c, asc, fld, digits
'' 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
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 == "r")
tx(CR)
elseif (c == "n")
tx(LF)
elseif (c == "t")
tx(TAB)
elseif (c == "q")
tx(34)
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)
else
tx(c)
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.dec(value, 0))
str(nstr.itoa(value, 10, 0))
PUB fwdec(value, digits)
'' Emit value as decimal using fixed # of digits
'' -- may add leading zeros
' str(nstr.dec(value, digits))
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 fwhex(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 fwoct(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 fwqrt(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 fwbin(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))
CON { license }
{{
-------------------------------------------------------------------------------------------------
MIT License
Copyright (c) 2022 Iron Sheep Productions, LLC
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 NONINFRINGEMENT. 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.
=================================================================================================
}}