Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
B
buspirate-python
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Dorota Czaplejewicz
buspirate-python
Commits
57c49c47
Commit
57c49c47
authored
Apr 16, 2017
by
Juergen Hasch
Committed by
GitHub
Apr 16, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5 from juhasch/feature/update
Fix autodetect and tests
parents
57dcc05b
ad9c0a1b
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
93 additions
and
51 deletions
+93
-51
README.md
README.md
+19
-2
pyBusPirateLite/BBIO_base.py
pyBusPirateLite/BBIO_base.py
+40
-17
pyBusPirateLite/BitBang.py
pyBusPirateLite/BitBang.py
+3
-1
pyBusPirateLite/I2C.py
pyBusPirateLite/I2C.py
+3
-1
pyBusPirateLite/SPI.py
pyBusPirateLite/SPI.py
+2
-0
pyBusPirateLite/UART.py
pyBusPirateLite/UART.py
+8
-3
spi_example.py
spi_example.py
+0
-18
tests/test_bbio_base.py
tests/test_bbio_base.py
+6
-0
tests/test_i2c.py
tests/test_i2c.py
+1
-5
tests/test_spi.py
tests/test_spi.py
+5
-2
tests/test_uart.py
tests/test_uart.py
+6
-2
No files found.
README.md
View file @
57c49c47
# pyBusPirateLite
Python library for BusPirate based on code from Garrett Berg. It tries to be more Pythonic than the original code.
Python library for BusPirate based on code from Garrett Berg.
It tries to be more Pythonic than the original code.
This library allows using the following modes:
*
SPI
...
...
@@ -31,8 +32,24 @@ Based on code from Garrett Berg <cloudform511@gmail.com>
data = spi.transfer( [0x82, 0x00])
spi.cs = False
### I2C

### Bitbang
from pyBusPirateLite.BitBang import BitBang
bb = BitBang()
bb.outputs = PIN_AUX
bb.pins = 0 # set aux pin = 0
bb.pins = PIN_AUX # set aux pin = 1
### Automatically detect port
from pyBusPirateLite.BitBang import BitBang
bb = BitBang(connect=False)
port = bb.get_port()
print(port)
### I2C
from pyBusPirateLite.SPI import
*
i2c = I2C()
...
...
pyBusPirateLite/BBIO_base.py
View file @
57c49c47
...
...
@@ -55,7 +55,7 @@ class BBIO_base:
_attempts_
=
0
# global stored for use in enter
def
enter
(
self
):
def
enter
_bb
(
self
):
"""Enter bitbang mode
This is the be-all-end-all restart function. It will keep trying
...
...
@@ -78,13 +78,10 @@ class BBIO_base:
IOError
If device is not connected
"""
if
self
.
mode
==
'bb'
:
return
if
self
.
connected
is
not
True
:
raise
IOError
(
'Device not connected'
)
self
.
timeout
(
self
.
minDelay
*
10
)
self
.
port
.
flushInput
()
for
i
in
range
(
10
):
self
.
write
(
0x00
)
r
=
self
.
response
(
1
,
True
)
...
...
@@ -99,7 +96,6 @@ class BBIO_base:
resp
=
self
.
response
(
200
)
self
.
write
(
0x00
)
resp
=
self
.
response
(
5
)
# print('RESP:', resp)
if
resp
==
"BBIO1"
:
self
.
mode
=
'bb'
self
.
bp_config
=
0x00
# configuration bits determine action of power sources and pullups
...
...
@@ -109,6 +105,13 @@ class BBIO_base:
return
True
raise
BPError
(
'Could not enter bitbang mode'
)
def
enter
(
self
):
"""Enter bitbang mode
Will be overriden by other classes
"""
if
self
.
mode
==
'bb'
:
return
return
self
.
enter_bb
()
def
hw_reset
(
self
):
"""Reset Bus Pirate
...
...
@@ -117,11 +120,41 @@ class BBIO_base:
The hardware and firmware version is printed (same as the 'i' command in the terminal),
and the Bus Pirate returns to the user terminal interface. Send 0x00 20 times to enter binary mode again.
"""
if
self
.
mode
!=
'bb'
:
self
.
enter_bb
()
self
.
write
(
0x0f
)
self
.
port
.
flushInput
()
self
.
timeout
(.
1
)
self
.
mode
=
None
def
get_port
(
self
):
"""Detect Buspirate and return first detected port
Returns
-------
str
First valid portname
"""
try
:
import
serial.tools.list_ports
as
list_ports
except
ImportError
:
raise
ImportError
(
'Pyserial version with serial.tools.list_port required'
)
import
serial
# the API in version 2 and 3 is different
if
serial
.
VERSION
[
0
]
==
'2'
:
ports
=
list_ports
.
comports
()
for
port
in
ports
:
if
len
(
port
)
==
3
and
'0403:6001'
in
port
[
2
]:
return
port
[
0
]
else
:
ports
=
list_ports
.
comports
()
for
port
in
ports
:
if
hasattr
(
port
,
'pid'
)
and
hasattr
(
port
,
'vid'
):
if
port
.
vid
==
1027
and
port
.
pid
==
24577
:
return
port
.
name
def
connect
(
self
,
portname
=
''
,
speed
=
115200
,
timeout
=
0.1
):
""" will try to automatically find a port regardless of os
...
...
@@ -142,18 +175,9 @@ class BBIO_base:
If device could not be opened
"""
try
:
import
serial.tools.list_ports
as
list_ports
except
ImportError
:
raise
ImportError
(
'Pyserial version with serial.tools.list_port required (> 3.0'
)
if
portname
==
''
:
ports
=
list_ports
.
comports
()
for
port
in
ports
:
if
hasattr
(
port
,
'pid'
)
and
hasattr
(
port
,
'vid'
):
if
port
.
vid
==
1027
and
port
.
pid
==
24577
:
portname
=
port
.
device
break
portname
=
self
.
get_port
()
self
.
portname
=
portname
try
:
self
.
port
=
serial
.
Serial
(
portname
,
speed
,
timeout
=
timeout
)
...
...
@@ -171,7 +195,6 @@ class BBIO_base:
sleep
(
timeout
)
def
write
(
self
,
value
):
# print('val %s' % value.to_bytes(1, 'big'))
self
.
port
.
write
(
value
.
to_bytes
(
1
,
'big'
))
def
response
(
self
,
byte_count
=
1
,
binary
=
False
):
...
...
pyBusPirateLite/BitBang.py
View file @
57c49c47
...
...
@@ -26,7 +26,7 @@ import serial
from
.BBIO_base
import
BBIO_base
,
BPError
,
ProtocolError
class
BitBang
(
BBIO_base
):
def
__init__
(
self
,
portname
=
''
,
speed
=
115200
,
timeout
=
1
,
connect
=
True
):
def
__init__
(
self
,
portname
=
''
,
speed
=
115200
,
timeout
=
0.
1
,
connect
=
True
):
""" Provide access to the Bus Pirate bitbang mode
Parameters
...
...
@@ -37,6 +37,8 @@ class BitBang(BBIO_base):
Communication speed, use default of 115200
timeout : int
Timeout in s to wait for reply
connect : bool
Automatically connect to BusPirate (default)
Example
-------
...
...
pyBusPirateLite/I2C.py
View file @
57c49c47
...
...
@@ -35,7 +35,7 @@ pin_mapping = {'AUX': 0b10,
class
I2C
(
BBIO_base
):
def
__init__
(
self
,
portname
=
''
,
speed
=
115200
,
timeout
=
1
,
connect
=
True
):
def
__init__
(
self
,
portname
=
''
,
speed
=
115200
,
timeout
=
0.
1
,
connect
=
True
):
""" Provide access to the Bus Pirate I2C interface
Parameters
...
...
@@ -71,6 +71,8 @@ class I2C(BBIO_base):
"""
if
self
.
mode
==
'i2c'
:
return
if
self
.
mode
!=
'bb'
:
super
(
I2C
,
self
).
enter
()
self
.
write
(
0x02
)
self
.
timeout
(
self
.
minDelay
*
10
)
if
self
.
response
(
4
)
==
"I2C1"
:
...
...
pyBusPirateLite/SPI.py
View file @
57c49c47
...
...
@@ -60,6 +60,8 @@ class SPI(BBIO_base):
Communication speed, use default of 115200
timeout : int
Timeout in s to wait for reply
connect : bool
Automatically connect to BusPirate (default)
Example
-------
...
...
pyBusPirateLite/UART.py
View file @
57c49c47
...
...
@@ -49,7 +49,7 @@ class UARTSpeed:
class
UART
(
BBIO_base
):
def
__init__
(
self
,
portname
=
''
,
speed
=
115200
,
timeout
=
1
):
def
__init__
(
self
,
portname
=
''
,
speed
=
115200
,
timeout
=
0.1
,
connect
=
True
):
""" Provide the Bus Pirate UART interface
Parameters
...
...
@@ -60,14 +60,17 @@ class UART(BBIO_base):
Communication speed, use default of 115200
timeout : int
Timeout in s to wait for reply
connect : bool
Automatically connect to BusPirate (default)
Example
-------
>>> spi = UART()
"""
super
().
__init__
()
self
.
connect
(
portname
,
speed
,
timeout
)
self
.
enter
()
if
connect
is
True
:
self
.
connect
(
portname
,
speed
,
timeout
)
self
.
enter
()
self
.
_config
=
None
self
.
_echo
=
False
...
...
@@ -81,6 +84,8 @@ class UART(BBIO_base):
"""
if
self
.
mode
==
'uart'
:
return
if
self
.
mode
!=
'bb'
:
super
(
UART
,
self
).
enter
()
self
.
write
(
0x03
)
self
.
timeout
(
self
.
minDelay
*
10
)
if
self
.
response
(
4
)
==
"ART1"
:
...
...
spi_example.py
deleted
100644 → 0
View file @
57dcc05b
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Example of SPI data transfer
from
pyBusPirateLite.SPI
import
*
spi
=
SPI
()
spi
.
pins
=
PIN_POWER
|
PIN_CS
spi
.
config
=
CFG_PUSH_PULL
spi
.
speed
=
'1MHz'
# send two bytes and receive answer
spi
.
cs
=
True
data
=
spi
.
transfer
(
[
0x82
,
0x55
])
spi
.
cs
=
False
spi
.
pins
=
PIN_CS
tests/test_bbio_base.py
View file @
57c49c47
...
...
@@ -11,6 +11,7 @@ def test_outputs():
bb
=
BitBang
()
bb
.
outputs
bb
.
disconnect
()
bb
.
hw_reset
()
def
test_pins_CS
():
...
...
@@ -25,6 +26,7 @@ def test_pins_CS():
sleep
(
0.2
)
assert
bb
.
outputs
==
PIN_CS
assert
bb
.
pins
==
PIN_CS
bb
.
hw_reset
()
def
test_pins_MISO
():
...
...
@@ -39,6 +41,7 @@ def test_pins_MISO():
sleep
(
0.2
)
assert
bb
.
outputs
==
PIN_MISO
assert
bb
.
pins
==
PIN_MISO
bb
.
hw_reset
()
def
test_pins_MOSI
():
...
...
@@ -53,6 +56,7 @@ def test_pins_MOSI():
sleep
(
0.2
)
assert
bb
.
outputs
==
PIN_MOSI
assert
bb
.
pins
==
PIN_MOSI
bb
.
hw_reset
()
def
test_pins_CLK
():
...
...
@@ -67,6 +71,7 @@ def test_pins_CLK():
sleep
(
0.2
)
assert
bb
.
outputs
==
PIN_CLK
assert
bb
.
pins
==
PIN_CLK
bb
.
hw_reset
()
def
test_pins_AUX
():
...
...
@@ -81,3 +86,4 @@ def test_pins_AUX():
bb
.
pins
=
PIN_AUX
assert
bb
.
outputs
==
PIN_AUX
assert
bb
.
pins
==
PIN_AUX
bb
.
hw_reset
()
tests/test_i2c.py
View file @
57c49c47
...
...
@@ -10,6 +10,7 @@ def test_connect():
i2c
=
I2C
(
connect
=
False
)
i2c
.
connect
()
assert
i2c
.
portname
!=
''
i2c
.
hw_reset
()
def
test_enter
():
...
...
@@ -24,11 +25,6 @@ def test_connect_on_init():
assert
i2c
.
mode
==
'i2c'
def
test_modestring
():
i2c
=
I2C
()
assert
i2c
.
modestring
==
'I2C1'
def
test_echo
():
pass
...
...
tests/test_spi.py
View file @
57c49c47
from
pyBusPirateLite.SPI
import
SPI
,
CFG_PUSH_PULL
,
CFG_IDLE
from
pyBusPirateLite.BBIO_base
import
PIN_POWER
,
PIN_CS
def
test_init
():
...
...
@@ -10,7 +9,8 @@ def test_init():
def
test_connect
():
spi
=
SPI
(
connect
=
False
)
spi
.
connect
()
assert
bb
.
portname
!=
''
assert
spi
.
portname
!=
''
spi
.
hw_reset
()
def
test_enter
():
...
...
@@ -18,13 +18,16 @@ def test_enter():
spi
.
connect
()
spi
.
enter
()
assert
spi
.
mode
==
'spi'
spi
.
hw_reset
()
def
test_connect_on_init
():
spi
=
SPI
()
assert
spi
.
mode
==
'spi'
spi
.
hw_reset
()
def
test_modestring
():
spi
=
SPI
()
assert
spi
.
modestring
==
'SPI1'
spi
.
hw_reset
()
tests/test_uart.py
View file @
57c49c47
...
...
@@ -9,7 +9,8 @@ def test_init():
def
test_connect
():
uart
=
UART
(
connect
=
False
)
uart
.
connect
()
assert
bb
.
portname
!=
''
assert
uart
.
portname
!=
''
uart
.
hw_reset
()
def
test_enter
():
...
...
@@ -17,16 +18,19 @@ def test_enter():
uart
.
connect
()
uart
.
enter
()
assert
uart
.
mode
==
'uart'
uart
.
hw_reset
()
def
test_connect_on_init
():
uart
=
UART
()
assert
UART
.
mode
==
'uart'
assert
uart
.
mode
==
'uart'
uart
.
hw_reset
()
def
test_modestring
():
uart
=
UART
()
assert
uart
.
modestring
==
'ART1'
uart
.
hw_reset
()
def
test_echo
():
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment