@@ -209,12 +209,28 @@ def __init__(self, baudrate = None, cs_gpio_port = None, cs_gpio_pin = None):
209209 self ._baudrate = baudrate
210210 self ._cs_gpio_port = cs_gpio_port
211211 self ._cs_gpio_pin = cs_gpio_pin
212+ self ._gpioport = 0
213+ self ._gpiopin = 0
214+ self ._gpiomode = 0
212215
213216 # Load DLL from default directory without any debugging prints
214217 self .sio = LIBUSBSIO ()
215218
216219 # Get number of LIBUSBSIO devices
217- res = self .sio .GetNumPorts ()
220+ res = self .sio .GetNumPorts (pidvids = [LIBUSBSIO .PIDVID_LPCLINK2 ])
221+ if res != 0 :
222+ self ._gpioport = 1
223+ self ._gpiopin = 2
224+ self ._gpiomode = 1
225+ else :
226+ res = self .sio .GetNumPorts (pidvids = [LIBUSBSIO .PIDVID_MCULINK ])
227+ if res != 0 :
228+ self ._gpioport = 0
229+ self ._gpiopin = 4
230+ self ._gpiomode = 0x100
231+ else :
232+ print ('No LIBUSBSIO devices found \r \n ' )
233+ return
218234 print ('Total LIBUSBSIO devices: %d \r \n ' % res )
219235
220236 # Open device at index 0
@@ -237,11 +253,11 @@ def __init__(self, baudrate = None, cs_gpio_port = None, cs_gpio_pin = None):
237253 self ._hSPIPort = self .sio .SPI_Open (int (self ._baudrate ), portNum = 0 , dataSize = 8 , preDelay = 0 )
238254
239255 # Configure GPIO pin for SPI master-slave signalling
240- res = self .sio .GPIO_ConfigIOPin (1 , 2 , 1 )
256+ res = self .sio .GPIO_ConfigIOPin (self . _gpioport , self . _gpiopin , self . _gpiomode )
241257 print ('GPIO_ConfigIOPin res: %d \r \n ' % res )
242- res = self .sio .GPIO_SetPortInDir (1 , 2 )
258+ res = self .sio .GPIO_SetPortInDir (self . _gpioport , self . _gpiopin )
243259 print ('GPIO_SetPortInDir res: %d \r \n ' % res )
244- res = self .sio .GPIO_GetPin (1 , 2 )
260+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
245261 print ('GPIO_GetPin res: %d \r \n ' % res )
246262
247263 def close (self ):
@@ -252,9 +268,9 @@ def close(self):
252268
253269 def _base_send (self , message ):
254270 # Wait for SPI master-slave signalling GPIO pin to be in low state
255- res = self .sio .GPIO_GetPin (1 , 2 )
271+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
256272 while (1 == res ):
257- res = self .sio .GPIO_GetPin (1 , 2 )
273+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
258274 # Send the header first
259275 data , rxbytesnumber = self ._hSPIPort .Transfer (0 , 15 , message [:self .HEADER_LEN ], self .HEADER_LEN , 0 )
260276 if rxbytesnumber > 0 :
@@ -265,10 +281,10 @@ def _base_send(self, message):
265281 print ('SPI transfer error: %d' % rxbytesnumber )
266282
267283 def _base_receive (self , count ):
268- # Wait for SPI master-slave signalling GPIO pin to be in low state
269- res = self .sio .GPIO_GetPin (1 , 2 )
284+ # Wait for SPI master-slave signalling GPIO pin to be in low state
285+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
270286 while (1 == res ):
271- res = self .sio .GPIO_GetPin (1 , 2 )
287+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
272288 data , rxbytesnumber = self ._hSPIPort .Transfer (0 , 15 , range (count ), count , 0 )
273289 if rxbytesnumber > 0 :
274290 #print('SPI received %d number of bytes' % rxbytesnumber)
@@ -290,12 +306,28 @@ def __init__(self, baudrate = None):
290306 baudrate = 100000
291307
292308 self ._baudrate = baudrate
309+ self ._gpioport = 0
310+ self ._gpiopin = 0
311+ self ._gpiomode = 0
293312
294313 # Load DLL from default directory without any debugging prints
295314 self .sio = LIBUSBSIO ()
296315
297316 #Get number of LIBUSBSIO devices
298- res = self .sio .GetNumPorts ()
317+ res = self .sio .GetNumPorts (pidvids = [LIBUSBSIO .PIDVID_LPCLINK2 ])
318+ if res != 0 :
319+ self ._gpioport = 1
320+ self ._gpiopin = 2
321+ self ._gpiomode = 1
322+ else :
323+ res = self .sio .GetNumPorts (pidvids = [LIBUSBSIO .PIDVID_MCULINK ])
324+ if res != 0 :
325+ self ._gpioport = 1
326+ self ._gpiopin = 3
327+ self ._gpiomode = 0x100
328+ else :
329+ print ('No LIBUSBSIO devices found \r \n ' )
330+ return
299331 print ('Total LIBUSBSIO devices: %d \r \n ' % res )
300332
301333 # Open device at index 0
@@ -318,11 +350,11 @@ def __init__(self, baudrate = None):
318350 self ._hI2CPort = self .sio .I2C_Open (int (self ._baudrate ), 0 , 0 )
319351
320352 # Configure GPIO pin for I2C master-slave signalling
321- res = self .sio .GPIO_ConfigIOPin (1 , 2 , 1 )
353+ res = self .sio .GPIO_ConfigIOPin (self . _gpioport , self . _gpiopin , self . _gpiomode )
322354 print ('GPIO_ConfigIOPin res: %d \r \n ' % res )
323- res = self .sio .GPIO_SetPortInDir (1 , 2 )
355+ res = self .sio .GPIO_SetPortInDir (self . _gpioport , self . _gpiopin )
324356 print ('GPIO_SetPortInDir res: %d \r \n ' % res )
325- res = self .sio .GPIO_GetPin (1 , 2 )
357+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
326358 print ('GPIO_GetPin res: %d \r \n ' % res )
327359
328360 def close (self ):
@@ -333,9 +365,9 @@ def close(self):
333365
334366 def _base_send (self , message ):
335367 # Wait for I2C master-slave signalling GPIO pin to be in low state
336- res = self .sio .GPIO_GetPin (1 , 2 )
368+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
337369 while (1 == res ):
338- res = self .sio .GPIO_GetPin (1 , 2 )
370+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
339371 # Send the header first
340372 data , rxbytesnumber = self ._hI2CPort .FastXfer (0x7E , message [:self .HEADER_LEN ], self .HEADER_LEN , 0 , 0 )
341373 if rxbytesnumber > 0 :
@@ -347,9 +379,9 @@ def _base_send(self, message):
347379
348380 def _base_receive (self , count ):
349381 # Wait for I2C master-slave signalling GPIO pin to be in low state
350- res = self .sio .GPIO_GetPin (1 , 2 )
382+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
351383 while (1 == res ):
352- res = self .sio .GPIO_GetPin (1 , 2 )
384+ res = self .sio .GPIO_GetPin (self . _gpioport , self . _gpiopin )
353385 # Issue the I2C_Transfer API
354386 data , rxbytesnumber = self ._hI2CPort .FastXfer (0x7E , 0 , 0 , count , 0 )
355387 if rxbytesnumber > 0 :
0 commit comments