forked from parallaxinc/spin-standard-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcom.spi.25khz.nocog.spin
237 lines (213 loc) · 9.25 KB
/
com.spi.25khz.nocog.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
{
--------------------------------------------
Filename: com.spi.25khz.nocog.spin
Author: Jesse Burt
Description: SPI engine (SPIN-based)
@80MHz Fsys:
Write speed: 25.641kHz actual (25% duty - 10uS H : 29uS L)
Read speed: 26.315kHz actual (26% duty - 10uS H : 28uS L)
Started 2009
Updated Jun 23, 2023
See end of file for terms of use.
--------------------------------------------
NOTE: This is based on SPI_Spin.spin,
originally by Beau Schwabe
}
VAR
long _SCK, _MOSI, _MISO
long _sck_delay
PUB null{}
' This is not a top-level object
PUB init(SCK, MOSI, MISO, SPI_MODE): status
' Initialize SPI engine using custom pins
' SCK, MOSI, MISO: 0..31 (each unique)
' SPI_MODE: 0..3
' 0: CPOL 0, CPHA 0
' SCK idles low
' MISO shifted in on rising clock pulse
' MOSI shifted out on falling clock pulse
' 1: CPOL 0, CPHA 1
' SCK idles low
' MISO shifted in on falling clock pulse
' MOSI shifted out on rising clock pulse
' 2: CPOL 1, CPHA 0
' SCK idles high
' MISO shifted in on falling clock pulse
' MOSI shifted out on rising clock pulse
' 3: CPOL 1, CPHA 1
' SCK idles high
' MISO shifted in on rising clock pulse
' MOSI shifted out on falling clock pulse
' NOTE: CS must be handled by the parent object
longmove(@_SCK, @SCK, 4) ' Copy pins
mode(SPI_MODE)
outa[SCK] := _cpol
dira[SCK] := 1
if ( lookdown(MOSI: 0..31) ) ' MOSI optional (read-only devices)
outa[MOSI] := 0
dira[MOSI] := 1
if ( lookdown(MISO: 0..31) ) ' MISO optional (write-only devices)
dira[MISO] := 0
return cogid{}+1 ' return current cog id
PUB deinit
' Deinitialize
' Float I/O pins and clear out hub vars
dira[_SCK] := 0
dira[_MOSI] := 0
dira[_MISO] := 0
longfill(@_SCK, 0, 6)
PUB rdbits_lsbf(nr_bits): val | SCK, MOSI, MISO, clk_delay, b
' Read arbitrary number of bits from SPI bus, least-significant bit first
' nr_bits: 1 to 32
ifnot (lookdown(nr_bits: 1..32)) ' reject invalid # bits
return
longmove(@SCK, @_SCK, 4)
val := 0
dira[MISO] := 0
case _spi_mode
0, 2:
repeat b from 0 to (nr_bits-1)
val |= (ina[MISO] << b)
!outa[SCK]
!outa[SCK]
1, 3:
repeat b from 0 to (nr_bits-1)
!outa[SCK]
val |= (ina[MISO] << b)
!outa[SCK]
PUB rdbits_msbf(nr_bits): val | SCK, MOSI, MISO, clk_delay, b
' Read arbitrary number of bits from SPI bus, most-significant bit first
' nr_bits: 1 to 32
ifnot (lookdown(nr_bits: 1..32)) ' reject invalid # bits
return
longmove(@SCK, @_SCK, 4)
val := 0
dira[MISO] := 0
case _spi_mode
0, 2:
repeat b from (nr_bits-1) to 0
val |= (ina[MISO] << b)
!outa[SCK]
!outa[SCK]
1, 3:
repeat b from (nr_bits-1) to 0
!outa[SCK]
val |= (ina[MISO] << b)
!outa[SCK]
PUB rdblock_lsbf(ptr_buff, nr_bytes) | SCK, MOSI, MISO, b_num, tmp
' Read block of data from SPI bus, least-significant byte first
longmove(@SCK, @_SCK, 4) ' copy pins from hub
dira[MISO] := 0 ' ensure MISO is an input
tmp := 0
case _spi_mode
0, 2:
repeat b_num from 0 to nr_bytes-1 ' byte loop
repeat 8 ' bit loop
tmp := tmp << 1 | ina[MISO] ' sample bit
!outa[SCK] ' clock
!outa[SCK]
byte[ptr_buff][b_num] := tmp ' copy working byte
1, 3:
repeat b_num from 0 to nr_bytes-1 ' byte loop
repeat 8 ' bit loop
!outa[SCK] ' clock
tmp := tmp << 1 | ina[MISO] ' sample bit
!outa[SCK]
byte[ptr_buff][b_num] := tmp ' copy working byte
PUB rdblock_msbf(ptr_buff, nr_bytes) | SCK, MOSI, MISO, b_num, tmp
' Read block of data from SPI bus, most-significant byte first
longmove(@SCK, @_SCK, 4) ' copy pins from hub
dira[MISO] := 0 ' ensure MISO is an input
tmp := 0
case _spi_mode
0, 2:
repeat b_num from nr_bytes-1 to 0 ' byte loop
repeat 8 ' bit loop
tmp := (tmp << 1) | ina[MISO] ' sample bit
!outa[SCK] ' clock
!outa[SCK]
byte[ptr_buff][b_num] := tmp ' copy working byte
1, 3:
repeat b_num from nr_bytes-1 to 0 ' byte loop
repeat 8 ' bit loop
!outa[SCK] ' clock
tmp := (tmp << 1) | ina[MISO] ' sample bit
!outa[SCK]
byte[ptr_buff][b_num] := tmp ' copy working byte
PUB wrbits_lsbf(val, nr_bits) | SCK, MOSI, MISO, clk_delay, b
' Write arbitrary number of bits to SPI bus, least-significant byte first
' nr_bits: 1 to 32
ifnot (lookdown(nr_bits: 1..32)) ' reject invalid # bits
return
longmove(@SCK, @_SCK, 4)
outa[MOSI] := 0
repeat b from 0 to (nr_bits-1)
outa[MOSI] := (val >> b)
!outa[SCK]
!outa[SCK]
PUB wrbits_msbf(val, nr_bits) | SCK, MOSI, MISO, clk_delay, b
' Write arbitrary number of bits to SPI bus, most-significant byte first
' nr_bits: 1 to 32
ifnot (lookdown(nr_bits: 1..32)) ' reject invalid # bits
return
longmove(@SCK, @_SCK, 4)
outa[MOSI] := 0
repeat b from (nr_bits-1) to 0
outa[MOSI] := (val >> b)
!outa[SCK]
!outa[SCK]
PUB wrblock_lsbf(ptr_buff, nr_bytes) | SCK, MOSI, MISO, b_num, tmp
' Write block of data to SPI bus from ptr_buff, least-significant byte first
longmove(@SCK, @_SCK, 4) ' copy pins from hub
dira[MOSI] := 1 ' ensure MOSI is an output
case _spi_mode
0, 2:
repeat b_num from 0 to nr_bytes-1 ' byte loop
tmp := (byte[ptr_buff][b_num] << 24)' align byte with MSBit of long
repeat 8 ' bit loop
outa[MOSI] := (tmp <-= 1) & 1 ' next bit into pos and isolate it
!outa[SCK] ' clock
!outa[SCK]
1, 3:
repeat b_num from 0 to nr_bytes-1 ' byte loop
tmp := (byte[ptr_buff][b_num] << 24)' align byte with MSBit of long
repeat 8 ' bit loop
outa[MOSI] := (tmp <-= 1) & 1 ' next bit into pos and isolate it
!outa[SCK] ' clock
!outa[SCK]
PUB wrblock_msbf(ptr_buff, nr_bytes) | SCK, MOSI, MISO, b_num, tmp
' Write block of data to SPI bus from ptr_buff, most-significant byte first
longmove(@SCK, @_SCK, 4) ' copy pins from hub
dira[MOSI] := 1 ' ensure MOSI is an output
case _spi_mode
0, 2:
repeat b_num from nr_bytes-1 to 0 ' byte loop
tmp := (byte[ptr_buff][b_num] << 24)' align byte with MSBit of long
repeat 8 ' bit loop
outa[MOSI] := (tmp <-= 1) & 1 ' next bit into pos and isolate it
!outa[SCK] ' clock
!outa[SCK]
1, 3:
repeat b_num from nr_bytes-1 to 0 ' byte loop
tmp := (byte[ptr_buff][b_num] << 24)' align byte with MSBit of long
repeat 8 ' bit loop
outa[MOSI] := (tmp <-= 1) & 1 ' next bit into pos and isolate it
!outa[SCK] ' clock
!outa[SCK]
#include "com.spi.common.spinh" ' R/W methods common to all SPI engines
DAT
{
Copyright 2022 Jesse Burt
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.
}