33import board
44import busio
55from digitalio import DigitalInOut , Direction , Pull
6+ from adafruit_bus_device .spi_device import SPIDevice
67from micropython import const
78
89class ESP_SPIcontrol :
@@ -72,17 +73,15 @@ def __init__(self, spi, cs_pin, ready_pin, reset_pin, gpio0_pin, *, debug=False)
7273 self ._buffer = bytearray (10 )
7374 self ._pbuf = bytearray (1 ) # buffer for param read
7475
75- self ._spi = spi
76+ self ._spi_device = SPIDevice ( spi , cs_pin , baudrate = 4000000 )
7677 self ._cs = cs_pin
7778 self ._ready = ready_pin
7879 self ._reset = reset_pin
7980 self ._gpio0 = gpio0_pin
80-
8181 self ._cs .direction = Direction .OUTPUT
8282 self ._ready .direction = Direction .INPUT
8383 self ._reset .direction = Direction .OUTPUT
8484 self ._gpio0 .direction = Direction .INPUT
85-
8685 self .reset ()
8786
8887 def reset (self ):
@@ -98,24 +97,6 @@ def reset(self):
9897
9998 self ._gpio0 .direction = Direction .INPUT
10099
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-
119100 def wait_for_ready (self ):
120101 if self ._debug :
121102 print ("Wait for ESP32 ready" , end = '' )
@@ -128,7 +109,6 @@ def wait_for_ready(self):
128109 time .sleep (0.01 )
129110 else :
130111 raise RuntimeError ("ESP32 not responding" )
131-
132112 if self ._debug :
133113 print ()
134114
@@ -155,59 +135,70 @@ def send_command(self, cmd, params=None, *, param_len_16=False):
155135 packet .append (0xFF )
156136
157137 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 ])
163148
164- def read_byte (self ):
165- self . _spi .readinto (self ._pbuf )
149+ def read_byte (self , spi ):
150+ spi .readinto (self ._pbuf )
166151 if self ._debug >= 2 :
167152 print ("\t \t Read:" , hex (self ._pbuf [0 ]))
168153 return self ._pbuf [0 ]
169154
170- def wait_spi_char (self , desired ):
155+ def wait_spi_char (self , spi , desired ):
171156 times = time .monotonic ()
172157 while (time .monotonic () - times ) < 0.1 :
173- r = self .read_byte ()
158+ r = self .read_byte (spi )
174159 if r == ERR_CMD :
175160 raise RuntimeError ("Error response to command" )
176161 if r == desired :
177162 return True
178163 else :
179164 raise RuntimeError ("Timed out waiting for SPI char" )
180165
181- def check_data (self , desired ):
182- r = self .read_byte ()
166+ def check_data (self , spi , desired ):
167+ r = self .read_byte (spi )
183168 if r != desired :
184169 raise RuntimeError ("Expected %02X but got %02X" % (desired , r ))
185170
186171 def wait_response_cmd (self , cmd , num_responses = None , * , param_len_16 = False ):
187172 self .wait_for_ready ()
188- self ._spi_select ()
189173
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 ()
196174 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 )
209201
210- self ._spi_deselect ()
211202 if self ._debug :
212203 print ("Read: " , responses )
213204 return responses
0 commit comments