forked from totalspectrum/spin2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleSerial.spin
More file actions
115 lines (103 loc) · 2.71 KB
/
Copy pathSimpleSerial.spin
File metadata and controls
115 lines (103 loc) · 2.71 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
''
'' serial port definitions
''
'' this is for a very simple serial port
'' also note: if PC is defined then we
'' substitute some C code instead
''
#ifdef __P2__
CON
clkfreq = 80_000_000
#endif
VAR
byte txpin
byte rxpin
long baud
long txmask
long bitcycles
#ifdef __SPIN2CPP__
'' we will be using stdio, so force it to
'' be included
{++
#include <stdio.h>
}
#endif
''
'' code: largely taken from FullDuplexSerial.spin
''
PUB start(rx_pin, tx_pin, mode, baudrate)
#ifndef PC
baud := baudrate
bitcycles := clkfreq / baudrate
txpin := tx_pin
txmask := (1<<txpin)
rxpin := rx_pin
#endif
return 1
PUB tx(c) | val, waitcycles, mask
#ifdef PC
'' just emit direct C code here
{++
putchar(c);
}
#else
mask := txmask
# ifdef __P2__
DIRB |= mask
OUTB |= mask
# else
DIRA |= mask
OUTA |= mask
#endif
val := (c | 256) << 1
waitcycles := CNT
repeat 10
waitcnt(waitcycles += bitcycles)
# ifdef __P2__
if (val & 1)
OUTB |= mask
else
OUTB &= !mask
# else
if (val & 1)
OUTA |= mask
else
OUTA &= !mask
# endif
val >>= 1
#endif
PUB str(s) | c
REPEAT WHILE ((c := byte[s++]) <> 0)
tx(c)
PUB dec(value) | i, x
'' Print a decimal number
result := 0
x := value == NEGX 'Check for max negative
if value < 0
value := ||(value+x) 'If negative, make positive; adjust for max negative
tx("-") 'and output sign
i := 1_000_000_000 'Initialize divisor
repeat 10 'Loop for 10 digits
if value => i
tx(value / i + "0" + x*(i == 1)) 'If non-zero digit, output digit; adjust for max negative
value //= i 'and digit from value
result~~ 'flag non-zero found
elseif result or i == 1
tx("0") 'If zero digit (or only digit) output it
i /= 10 'Update divisor
PUB hex(val, digits) | shft, x
shft := (digits - 1) << 2
repeat digits
x := (val >> shft) & $F
shft -= 4
if (x => 10)
x := (x - 10) + "A"
else
x := x + "0"
tx(x)
''
'' could signal back to the host with an exit code
'' for now don't
''
PUB exit(code)
repeat