-
Notifications
You must be signed in to change notification settings - Fork 3
/
PropellerLoader.spin
212 lines (156 loc) · 13.2 KB
/
PropellerLoader.spin
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
''***************************************
''* Propeller Loader v1.0 *
''* Author: Chip Gracey *
''* Copyright (c) 2006 Parallax, Inc. *
''* See end of file for terms of use. *
''***************************************
' v1.0 - 13 June 2006 - original version
''_____________________________________________________________________________
''
''This object lets a Propeller chip load up another Propeller chip in the same
''way the PC normally does.
''
''To do this, the program to be loaded into the other Propeller chip must be
''compiled using "F8" (be sure to enable "Show Hex") and then a "Save Binary
''File" must be done. This binary file must then be included so that it will be
''resident and its address can be conveyed to this object for loading.
''
''Say that the file was saved as "loadme.binary". Also, say that the Propeller
''which will be performing the load/program operation has its pins 0..2 tied to
''the other Propeller's pins RESn, P31, and P30, respectively. And we'll say
''we're working with Version 1 chips and you just want to load and execute the
''program. Your code would look something like this:
''
''
''OBJ loader : "PropellerLoader"
''
''DAT loadme file "loadme.binary"
''
''PUB LoadPropeller
''
'' loader.Connect(0, 1, 2, 1, loader#LoadRun, @loadme)
''
''
''This object drives the other Propeller's RESn line, so it is recommended that
''the other Propeller's BOEn pin be tied high and that its RESn pin be pulled
''to VSS with a 1M resistor to keep it on ice until showtime.
''_____________________________________________________________________________
''
CON
#1, ErrorConnect, ErrorVersion, ErrorChecksum, ErrorProgram, ErrorVerify
#0, Shutdown, LoadRun, ProgramShutdown, ProgramRun
VAR
long P31, P30, LFSR, Ver, Echo
PUB Connect(PinRESn, PinP31, PinP30, Version, Command, CodePtr) : Error
'set P31 and P30
P31 := PinP31
P30 := PinP30
'RESn low
outa[PinRESn] := 0
dira[PinRESn] := 1
'P31 high (our TX)
outa[PinP31] := 1
dira[PinP31] := 1
'P30 input (our RX)
dira[PinP30] := 0
'RESn high
outa[PinRESn] := 1
'wait 100ms
waitcnt(clkfreq / 10 + cnt)
'Communicate (may abort with error code)
if Error := \Communicate(Version, Command, CodePtr)
dira[PinRESn] := 0
'P31 float
dira[PinP31] := 0
PRI Communicate(Version, Command, CodePtr) | ByteCount
'output calibration pulses
BitsOut(%01, 2)
'send LFSR pattern
LFSR := "P"
repeat 250
BitsOut(IterateLFSR, 1)
'receive and verify LFSR pattern
repeat 250
if WaitBit(1) <> IterateLFSR
abort ErrorConnect
'receive chip version
repeat 8
Ver := WaitBit(1) << 7 + Ver >> 1
'if version mismatch, shutdown and abort
if Ver <> Version
BitsOut(Shutdown, 32)
abort ErrorVersion
'send command
BitsOut(Command, 32)
'handle command details
if Command
'send long count
ByteCount := byte[CodePtr][8] | byte[CodePtr][9] << 8
BitsOut(ByteCount >> 2, 32)
'send bytes
repeat ByteCount
BitsOut(byte[CodePtr++], 8)
'allow 250ms for positive checksum response
if WaitBit(25)
abort ErrorChecksum
'eeprom program command
if Command > 1
'allow 5s for positive program response
if WaitBit(500)
abort ErrorProgram
'allow 2s for positive verify response
if WaitBit(200)
abort ErrorVerify
PRI IterateLFSR : Bit
'get return bit
Bit := LFSR & 1
'iterate LFSR (8-bit, $B2 taps)
LFSR := LFSR << 1 | (LFSR >> 7 ^ LFSR >> 5 ^ LFSR >> 4 ^ LFSR >> 1) & 1
PRI WaitBit(Hundredths) : Bit | PriorEcho
repeat Hundredths
'output 1t pulse
BitsOut(1, 1)
'sample bit and echo
Bit := ina[P30]
PriorEcho := Echo
'output 2t pulse
BitsOut(0, 1)
'if echo was low, got bit
if not PriorEcho
return
'wait 10ms
waitcnt(clkfreq / 100 + cnt)
'timeout, abort
abort ErrorConnect
PRI BitsOut(Value, Bits)
repeat Bits
if Value & 1
'output '1' (1t pulse)
outa[P31] := 0
Echo := ina[P30]
outa[P31] := 1
else
'output '0' (2t pulse)
outa[P31] := 0
outa[P31] := 0
Echo := ina[P30]
Echo := ina[P30]
outa[P31] := 1
Value >>= 1
{{
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ 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 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. │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
}}