-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathjm_ez_spi.spin2
More file actions
211 lines (152 loc) · 9.01 KB
/
Copy pathjm_ez_spi.spin2
File metadata and controls
211 lines (152 loc) · 9.01 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
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
'' =================================================================================================
''
'' File....... jm_ez_spi.spin2
'' Purpose.... Smart pin Mode 0 SPI routines
'' Author..... Jon "JonnyMac" McPhalen
'' Copyright (c) 2020 Jon McPhalen
'' -- see below for terms of use
'' E-mail..... jon.mcphalen@gmail.com
'' Started....
'' Updated.... 01 SEP 2020
''
'' {$P2}
''
'' =================================================================================================
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
#0, LSBFIRST, MSBFIRST
var
long setup
long sdi ' smart pin #s (-1 if not used)
long sdo
long sck
pub null()
'' This is not a top object
pub start(sdipin, sdopin, sckpin, khz) : result | m, x
pub start(sdipin, sdopin, sckpin, khz, cpha) : result | m, x
pub start(sdipin, sdopin, sckpin, khz, cpha, cpol) : result | m, x
'' Configure P2 smart pins for SPI coms
'' -- sdipin is input from external device (-1 if not used)
'' -- sdopin is output to external device (-1 if not used)
'' -- sckpin is clock output
'' -- khz is clock frequence in kilohertz (1000 = 1MHz)
stop()
if ((sdipin == sdopin) || (sckpin < 0)) ' validate pins
return false
longmove(@sdi, @sdipin, 3) ' save pins
if (sdi >= 0)
m := P_SYNC_RX ' spi rx mode
m |= ((sck-sdi) & %111) << 24 ' add SCK offset (B pin)
x := %0_00000 | (8-1) ' sample ahead of b pin rise, 8 bits
pinstart(sdi, m, x, 0) ' configure smart pin
m |= cpha << 27 ' set clock phase
pinf(sdi) ' disable until used
if (sdo >= 0)
m := P_OE | P_SYNC_TX ' spi tx mode
m |= ((sck-sdo) & %111) << 24 ' add SCK offset (B pin)
m |= cpha << 27 ' set clock phase
x := %1_00000 | (8-1) ' start/stop mode, 8 bits
pinstart(sdo, m, x, 0) ' configure smart pin
pinf(sdo) ' disable until used
m := P_OE | P_PULSE ' pulses for spi clock
x.word[0] := 2 #> (clkfreq / (khz*1000)) <# $FFFF ' ticks in period
x.word[1] := x.word[0] >> 1 ' ticks in low cycle (50%)
pinstart(sck, m, x, 0) ' configure smart pin
m |= cpol << 14 ' set clock polarity
setup := true
return setup
pub stop()
'' Clears SPI smart pins if configured
if (setup) ' configured?
if (sdi >= 0) ' clear smart pins
pinclear(sdi)
if (sdo >= 0)
pinclear(sdo)
pinclear(sck)
setup := false
longfill(@sdi, -1, 3) ' mark pins as unused
pub shiftin(mode, bits) : value
'' Shift data in from a synchronous serial device
'' -- mode is bit order: LSBFIRST or MSBFIRST
'' -- bits in the number of bits to shift
if (sdi < 0) ' abort if di not defined
return 0
pinf(sdi) ' reset di
wxpin(sdi, %0_00000 | (bits-1)) ' configure di sampling/bits
pinl(sdi) ' enable di
wypin(sck, bits) ' start clocking data
repeat until pinr(sck) ' wait until clocks finished
value := rdpin(sdi) ' get value
if (mode == LSBFIRST)
value >>= (32-bits) ' align lsb
else
value rev= 31 ' flip to align lsb
if (bits < 32) ' clear unused bits
value zerox= (bits-1)
pub shiftout(mode, value, bits)
'' Shift data out to a synchronous serial device
'' -- mode is bit order: LSBFIRST or MSBFIRST
'' -- value is shifted out
'' -- bits in the number of bits to shift
if (sdo < 0) ' abort if do not defined
return
if (mode == MSBFIRST) ' flip to output from LSB end
if (bits < 32) ' adjust alignment if needed
value <<= (32-bits)
value rev= 31
wxpin(sdo, %1_00000 | (bits-1)) ' configure do bits
wypin(sdo, value) ' load value
pinl(sdo) ' enable do
wypin(sck, bits) ' start clocking data
repeat until pinr(sck) ' wait for clocking to finish
pinf(sdo) ' disable do
pub shiftio(mode, outval, bits) : inval
'' Shift outval to a synchronous serial device while receiving inval
'' -- mode is bit order: LSBFIRST or MSBFIRST
'' -- bits in the number of bits to shift
if ((sdi < 0) || (sdo < 0)) ' abort if di and do not defined
return 0
if (mode == MSBFIRST)
if (bits < 32) ' adjust alignment if needed
outval <<= (32-bits)
outval rev= 31 ' flip to output from LSB end
wxpin(sdo, %1_00000 | (bits-1)) ' configure do bits
wypin(sdo, outval) ' load value
pinl(sdo) ' enable do
pinf(sdi) ' reset di
wxpin(sdi, %0_00000 | (bits-1)) ' configure di sampling/bits
pinl(sdi) ' enable di
wypin(sck, bits) ' start clocking data
repeat until pinr(sck) ' wait for clocking to finish
pinf(sdo) ' disable do
inval := rdpin(sdi) ' get inval
if (mode == LSBFIRST)
inval >>= (32-bits) ' align lsb
else
inval rev= 31 ' flip to align lsb
if (bits < 32) ' clear unused bits
inval zerox= (bits-1)
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.
}}