1
Bus Pirate Development / Re: pyBusPirateLite for Windows
I test
see you later
Open Source Hardware
This section allows you to view all Show Posts made by this member. Note that you can only see Show Posts made in areas you currently have access to.
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Peter Huewe on 2009-10-26.
Copyright 2009 Peter Huewe <peterhuewe@gmx.de>
Based on the spi testscript from Sean Nelson
This file is part of pyBusPirate.
pyBusPirate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pyBusPirate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
from pyBusPirateLite.I2C import *
""" enter binary mode """
def i2c_write_data(data):
i2c.send_start_bit()
i2c.bulk_trans(len(data),data)
i2c.send_stop_bit()
def i2c_read_bytes(address, numbytes, ret=False):
data_out=[]
i2c.send_start_bit()
i2c.bulk_trans(len(address),address)
while numbytes > 0:
if not ret:
print(ord(i2c.read_byte()))
else:
data_out.append(ord(i2c.read_byte()))
if numbytes > 1:
i2c.send_ack()
numbytes-=1
i2c.send_nack()
i2c.send_stop_bit()
if ret:
return data_out
if __name__ == '__main__':
i2c = I2C("com3", 115200)
print("Entering binmode: ", end=' ')
if i2c.BBmode() :
print("OK.") #truque return1/return0 dans BitBang
else:
print("failed.")
sys.exit()
print("Entering raw I2C mode: ", end=' ')
if i2c.enter_I2C:
print("OK.")
else:
print("failed.")
sys.exit()
print("Configuring I2C.")
if not i2c.cfg_pins(I2CPins.POWER | I2CPins.PULLUPS):
print("Failed to set I2C peripherals.")
sys.exit()
if not i2c.set_speed(I2CSpeed._50KHZ):
print("Failed to set I2C Speed.")
sys.exit()
i2c.timeout(0.2)
print("Reading EEPROM.")
i2c_write_data([0xa0, 0,0, 1, 2,3,4,5,6,7,8,9])
i2c_write_data([0xa0, 0,0])
print(i2c_read_bytes([0xa1],5))
print("Reset Bus Pirate to user terminal: ")
if i2c.resetBP():
print("OK.")
else:
print("failed.")
sys.exit()
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Sean Nelson on 2009-10-14.
Copyright 2009 Sean Nelson <audiohacked@gmail.com>
This file is part of pyBusPirate.
pyBusPirate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pyBusPirate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
from .BitBang import BBIO
class I2CSpeed:
_400KHZ = 0x03
_100KHZ = 0x02
_50KHZ = 0x01
_5KHZ = 0x00
class I2CPins:
POWER = 0x8
PULLUPS = 0x4
AUX = 0x2
CS = 0x1
class I2C(BBIO):
bulk_read = None
def __init__(self, port, speed, timeout=1):
BBIO.__init__(self, port, speed, timeout)
def send_start_bit(self):
self.port.write("x02")
#self.timeout(0.1)
return self.response()
def send_stop_bit(self):
self.port.write("x03")
#self.timeout(0.1)
return self.response()
def read_byte(self):
self.port.write("x04")
#self.timeout(0.1)
return self.response(1, True)
def send_ack(self):
self.port.write("x06")
#self.timeout(0.1)
return self.response()
def send_nack(self):
self.port.write("x07")
#self.timeout(0.1)
return self.response()
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Sean Nelson on 2009-10-14.
Copyright 2009 Sean Nelson <audiohacked@gmail.com>
This file is part of pyBusPirate.
pyBusPirate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pyBusPirate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
import select
import serial
import sys
import time
"""
PICSPEED = 24MHZ / 16MIPS
"""
class PinCfg:
POWER = 0x8
PULLUPS = 0x4
AUX = 0x2
CS = 0x1
class BBIOPins:
# Bits are assigned as such:
MOSI = 0x01;
CLK = 0x02;
MISO = 0x04;
CS = 0x08;
AUX = 0x10;
PULLUP = 0x20;
POWER = 0x40;
class BBIO:
def __init__(self, p="COM3", s=115200, t=1):
self.port = serial.Serial(p, s, timeout=t)
def BBmode(self):
if sys.platform == 'win32':
# I haven't seen this problem on Windows, which is good,
# because it doesn't appear to support select.select()
# for serial ports. This seems to work instead:
self.resetBP()
self.port.write(str("x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00x00").encode())
self.timeout(0.1)
self.port.flushInput();
self.reset()
else:
# Sometimes sending 20 zeroes in a row to a BP already in binary mode
# leads to BP getting stuck in BitBang mode
# (see forum: http://dangerousprototypes.com/forum/viewtopic.php?t=1440#msg13179 )
# so if we detect a response (indicating we're in BitBang mode before the 20 zeroes)
# stop sending zeroes and go on.
self.port.flushInput();
for i in range(20):
self.port.write(str("x00").encode());
r,w,e = select.select([self.port], [], [], 0.01);
#if (r): break;
if self.response(5) == "BBIO1": return 1
else: return 1 #normalement return 0=normally return 0 but not ok on w7 64bits
def reset(self):
self.port.write(str('x01').encode())
self.timeout(0.1)
def enter_SPI(self):
self.response(5)
self.port.write(str("x01").encode())
self.timeout(0.1)
if self.response(4) == "SPI1": return 1
else: return 0
def enter_I2C(self):
self.port.write(str("x02").encode())
self.timeout(0.1)
if self.response(4) == "I2C1": return 1
else: return 0
def enter_UART(self):
self.port.write(str("x03").encode())
self.timeout(0.1)
if self.response(4) == "ART1": return 1
else: return 0
def enter_1wire(self):
self.port.write(str("x04").encode())
self.timeout(0.1)
if self.response(4) == "1W01": return 1
else: return 0
def enter_rawwire(self):
self.port.write(str("x05").encode())
self.timeout(0.1)
if self.response(4) == "RAW1": return 1
else: return 0
def resetBP(self):
self.reset()
self.port.write(str("x0F").encode())
self.timeout(0.1)
#self.port.read(2000)
self.port.flushInput()
return 1
def raw_cfg_pins(self, config):
self.port.write(str(chr(0x40 | config)).encode())
self.timeout(0.1)
return self.response(1)
def raw_set_pins(self, pins):
self.port.write(str(chr(0x80 | config)).encode())
self.timeout(0.1)
return self.response(1)
def timeout(self, timeout=0.1):
time.sleep(timeout);
def response(self, byte_count=1, return_data=False):
data = self.port.read(byte_count)
if byte_count == 1 and return_data == False:
if data == chr(0x01): return 1 #to modify or not ?(str(chr0x01).encode()). For me not to modify
else: return 0
else:
return data
""" Self-Test """
def short_selftest(self):
self.port.write(str("x10").encode())
self.timeout(0.1)
return self.response(1, True)
def long_selftest(self):
self.port.write(str("x11").encode())
self.timeout(0.1)
return self.response(1, True)
""" PWM """
def setup_PWM(self, prescaler, dutycycle, period):
self.port.write(str("x12").encode())
self.port.write(str(chr(prescaler)).encode())
self.port.write(str(chr((dutycycle>>8)&0xFF)).encode())
self.port.write(str(chr(dutycycle&0xFF)).encode())
self.port.write(str(chr((period>>8)&0xFF)).encode())
self.port.write(str(chr(period&0xFF)).encode())
self.timeout(0.1)
return self.response()
def clear_PWM(self):
self.port.write(str("x13").encode())
self.timeout(0.1)
return self.response()
""" ADC """
def ADC_measure(self):
self.port.write(str("x14").encode())
self.timeout(0.1)
return self.response(2, True)
""" General Commands for Higher-Level Modes """
def mode_string(self):
self.port.write(str("x01").encode())
self.timeout(0.1)
return self.response()
def bulk_trans(self, byte_count=1, byte_string=None):
if byte_string == None: pass
self.port.write(str(chr(0x10 | (byte_count-1))).encode())
#self.timeout(0.1)
for i in range(byte_count):
self.port.write(str(chr(byte_string[i])).encode())
#self.timeout(0.1)
data = self.response(byte_count+1, True)
return data[1:]
def cfg_pins(self, pins=0):
self.port.write(str(chr(0x40 | pins)).encode())
self.timeout(0.1)
return self.response()
def read_pins(self):
self.port.write(str("x50").encode())
self.timeout(0.1)
return self.response(1, True)
def set_speed(self, spi_speed=0):
self.port.write(str(chr(0x60 | spi_speed)).encode())
self.timeout(0.1)
return self.response()
def read_speed(self):
self.port.write(str("x70").encode())
self.timeout(0.1);
return self.response(1, True)