3
3
import board
4
4
import busio
5
5
from digitalio import DigitalInOut , Direction , Pull
6
+ from adafruit_bus_device .spi_device import SPIDevice
6
7
from micropython import const
7
8
8
9
class ESP_SPIcontrol :
@@ -72,17 +73,15 @@ def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin, *, debug=False)
72
73
self ._buffer = bytearray (10 )
73
74
self ._pbuf = bytearray (1 ) # buffer for param read
74
75
75
- self ._spi = spi
76
+ self ._spi_device = SPIDevice ( spi , cs_pin , baudrate = 4000000 )
76
77
self ._cs = cs_pin
77
78
self ._ready = ready_pin
78
79
self ._reset = reset_pin
79
80
self ._gpio0 = gpio0_pin
80
-
81
81
self ._cs .direction = Direction .OUTPUT
82
82
self ._ready .direction = Direction .INPUT
83
83
self ._reset .direction = Direction .OUTPUT
84
84
self ._gpio0 .direction = Direction .INPUT
85
-
86
85
self .reset ()
87
86
88
87
def reset (self ):
@@ -98,24 +97,6 @@ def reset(self):
98
97
99
98
self ._gpio0 .direction = Direction .INPUT
100
99
101
- def _spi_select (self ):
102
- while not self ._spi .try_lock ():
103
- pass
104
- self ._spi .configure (baudrate = 100000 ) # start slow
105
- self ._cs .value = False # the actual select
106
- times = time .monotonic ()
107
- while (time .monotonic () - times ) < 1 : # wait up to 1000ms
108
- if self ._ready .value : # ok ready to send!
109
- return
110
- # some failure
111
- self ._cs .value = True
112
- self ._spi .unlock ()
113
- raise RuntimeError ("ESP32 timed out on SPI select" )
114
-
115
- def _spi_deselect (self ):
116
- self ._cs .value = True
117
- self ._spi .unlock ()
118
-
119
100
def wait_for_ready (self ):
120
101
if self ._debug :
121
102
print ("Wait for ESP32 ready" , end = '' )
@@ -128,7 +109,6 @@ def wait_for_ready(self):
128
109
time .sleep (0.01 )
129
110
else :
130
111
raise RuntimeError ("ESP32 not responding" )
131
-
132
112
if self ._debug :
133
113
print ()
134
114
@@ -155,59 +135,70 @@ def send_command(self, cmd, params=None, *, param_len_16=False):
155
135
packet .append (0xFF )
156
136
157
137
self .wait_for_ready ()
158
- self ._spi_select ()
159
- self ._spi .write (bytearray (packet ))
160
- if self ._debug :
161
- print ("Wrote: " , [hex (b ) for b in packet ])
162
- self ._spi_deselect ()
138
+ with self ._spi_device as spi :
139
+ times = time .monotonic ()
140
+ while (time .monotonic () - times ) < 1 : # wait up to 1000ms
141
+ if self ._ready .value : # ok ready to send!
142
+ break
143
+ else :
144
+ raise RuntimeError ("ESP32 timed out on SPI select" )
145
+ spi .write (bytearray (packet ))
146
+ if self ._debug :
147
+ print ("Wrote: " , [hex (b ) for b in packet ])
163
148
164
- def read_byte (self ):
165
- self . _spi .readinto (self ._pbuf )
149
+ def read_byte (self , spi ):
150
+ spi .readinto (self ._pbuf )
166
151
if self ._debug >= 2 :
167
152
print ("\t \t Read:" , hex (self ._pbuf [0 ]))
168
153
return self ._pbuf [0 ]
169
154
170
- def wait_spi_char (self , desired ):
155
+ def wait_spi_char (self , spi , desired ):
171
156
times = time .monotonic ()
172
157
while (time .monotonic () - times ) < 0.1 :
173
- r = self .read_byte ()
158
+ r = self .read_byte (spi )
174
159
if r == ERR_CMD :
175
160
raise RuntimeError ("Error response to command" )
176
161
if r == desired :
177
162
return True
178
163
else :
179
164
raise RuntimeError ("Timed out waiting for SPI char" )
180
165
181
- def check_data (self , desired ):
182
- r = self .read_byte ()
166
+ def check_data (self , spi , desired ):
167
+ r = self .read_byte (spi )
183
168
if r != desired :
184
169
raise RuntimeError ("Expected %02X but got %02X" % (desired , r ))
185
170
186
171
def wait_response_cmd (self , cmd , num_responses = None , * , param_len_16 = False ):
187
172
self .wait_for_ready ()
188
- self ._spi_select ()
189
173
190
- self .wait_spi_char (START_CMD )
191
- self .check_data (cmd | REPLY_FLAG )
192
- if num_responses is not None :
193
- self .check_data (num_responses )
194
- else :
195
- num_responses = self .read_byte ()
196
174
responses = []
197
- for num in range (num_responses ):
198
- response = []
199
- param_len = self .read_byte ()
200
- if param_len_16 :
201
- param_len <<= 8
202
- param_len |= self .read_byte ()
203
- if self ._debug >= 2 :
204
- print ("\t Parameter #%d length is %d" % (num , param_len ))
205
- for j in range (param_len ):
206
- response .append (self .read_byte ())
207
- responses .append (bytes (response ))
208
- self .check_data (END_CMD )
175
+ with self ._spi_device as spi :
176
+ times = time .monotonic ()
177
+ while (time .monotonic () - times ) < 1 : # wait up to 1000ms
178
+ if self ._ready .value : # ok ready to send!
179
+ break
180
+ else :
181
+ raise RuntimeError ("ESP32 timed out on SPI select" )
182
+
183
+ self .wait_spi_char (spi , START_CMD )
184
+ self .check_data (spi , cmd | REPLY_FLAG )
185
+ if num_responses is not None :
186
+ self .check_data (spi , num_responses )
187
+ else :
188
+ num_responses = self .read_byte (spi )
189
+ for num in range (num_responses ):
190
+ response = []
191
+ param_len = self .read_byte (spi )
192
+ if param_len_16 :
193
+ param_len <<= 8
194
+ param_len |= self .read_byte (spi )
195
+ if self ._debug >= 2 :
196
+ print ("\t Parameter #%d length is %d" % (num , param_len ))
197
+ for j in range (param_len ):
198
+ response .append (self .read_byte (spi ))
199
+ responses .append (bytes (response ))
200
+ self .check_data (spi , END_CMD )
209
201
210
- self ._spi_deselect ()
211
202
if self ._debug :
212
203
print ("Read: " , responses )
213
204
return responses
0 commit comments