def require_repeated_start(): """Enable repeated start conditions for I2C register reads. This is the normal behavior for I2C, however on some platforms like the Raspberry Pi there are bugs which disable repeated starts unless explicitly enabled with this function. See this thread for more details: http://www.raspberrypi.org/forums/viewtopic.php?f=44&t=15840 """ plat = Platform.platform_detect() if plat == Platform.RASPBERRY_PI: # On the Raspberry Pi there is a bug where register reads don't send a # repeated start condition like the kernel smbus I2C driver functions # define. As a workaround this bit in the BCM2708 driver sysfs tree can # be changed to enable I2C repeated starts. subprocess.check_call('chmod 666 /sys/module/i2c_bcm2708/parameters/combined', shell=True) subprocess.check_call('echo -n 1 > /sys/module/i2c_bcm2708/parameters/combined', shell=True)
def get_platform_gpio(**keywords): """Attempt to return a GPIO instance for the platform which the code is being executed on. Currently supports only the Raspberry Pi using the RPi.GPIO library and Beaglebone Black using the Adafruit_BBIO library. Will throw an exception if a GPIO instance can't be created for the current platform. The returned GPIO object is an instance of BaseGPIO. """ plat = Platform.platform_detect() if plat == Platform.RASPBERRY_PI: import RPi.GPIO return RPiGPIOAdapter(RPi.GPIO, **keywords) elif plat == Platform.BEAGLEBONE_BLACK: import Adafruit_BBIO.GPIO return AdafruitBBIOAdapter(Adafruit_BBIO.GPIO, **keywords) elif plat == Platform.UNKNOWN: raise RuntimeError('Could not determine platform.')
def get_platform_pwm(**keywords): """Attempt to return a PWM instance for the platform which the code is being executed on. Currently supports only the Raspberry Pi using the RPi.GPIO library and Beaglebone Black using the Adafruit_BBIO library. Will throw an exception if a PWM instance can't be created for the current platform. The returned PWM object has the same interface as the RPi_PWM_Adapter and BBIO_PWM_Adapter classes. """ plat = Platform.platform_detect() if plat == Platform.RASPBERRY_PI: import RPi.GPIO return RPi_PWM_Adapter(RPi.GPIO, **keywords) elif plat == Platform.BEAGLEBONE_BLACK: import Adafruit_BBIO.PWM return BBIO_PWM_Adapter(Adafruit_BBIO.PWM, **keywords) elif plat == Platform.UNKNOWN: raise RuntimeError('Could not determine platform.')
def get_default_bus(): """Return the default bus number based on the device platform. For a Raspberry Pi either bus 0 or 1 (based on the Pi revision) will be returned. For a Beaglebone Black the first user accessible bus, 1, will be returned. """ plat = Platform.platform_detect() if plat == Platform.RASPBERRY_PI: if Platform.pi_revision() == 1: # Revision 1 Pi uses I2C bus 0. return 0 else: # Revision 2 Pi uses I2C bus 1. return 1 elif plat == Platform.BEAGLEBONE_BLACK: # Beaglebone Black has multiple I2C buses, default to 1 (P9_19 and P9_20). return 1 else: raise RuntimeError('Could not determine default I2C bus for platform.')
import math import Platform from log import setup_logger from time import sleep PINS = [0, 2, 3, 21, 22, 23] RED_PIN = PINS[2] GREEN_PIN = PINS[1] BLUE_PIN = PINS[0] is_a_raspberryPi = Platform.platform_detect() == 1 is_gpio_enabled = False logger = setup_logger("Hardware Adapter") if is_a_raspberryPi: import wiringpi else: # if this is not a RPi you can't run wiringpi so lets load # something in its place import wiring_pi as wiringpi logger.info("Detected: Not running on a Raspberry Pi") wiringpi.wiringPiSetup() def enable_gpio(): global is_gpio_enabled """Attempts to take hold of the gpio from wiring pi.""" for pin in PINS: wiringpi.softPwmCreate(pin, 0, 100)
def end_early(): """atexit function""" if state == "random_pattern": exit_event.set() time.sleep(3) turn_off_lights() atexit.register(end_early) # Remove traceback on Ctrl-C signal.signal(signal.SIGINT, lambda x, y: sys.exit(0)) cm = configuration_manager.Configuration() is_a_raspberryPI = Platform.platform_detect() == 1 if is_a_raspberryPI: import wiringpi2 as wiringpi else: # if this is not a RPi you can't run wiringpi so lets load # something in its place import wiring_pi as wiringpi logging.debug("Not running on a raspberryPI") _PWM_MAX = cm.hardware.pwm_range _ACTIVE_LOW_MODE = cm.hardware.active_low_mode always_on_channels = cm.lightshow.always_on_channels always_off_channels = cm.lightshow.always_off_channels
def flash_nrf51(jtag, softdevice, bootloader, board, firmware): """Flash Bluefruit module Softdevice + Bootloader + Firmware""" if (board is None) or (board not in supported_boards): click.echo( "Please specify the board either " + ', '.join(supported_boards) ) sys.exit(1) if (board == "blefriend16"): board = "blefriend" bootloader = 0 # force legacy bootloader if firmware is None: click.echo("Please specify the firmware version e.g: 0.6.5") sys.exit(1) if jtag not in supported_programmers: click.echo("Unsupported programmer, please specify one of following: " + ', '.join(supported_programmers)) sys.exit(1) click.echo('jtag \t: %s' % jtag) click.echo('softdevice \t: %s' % softdevice) click.echo('bootloader \t: %s' % bootloader) click.echo('board \t: %s' % board) click.echo('firmware \t: %s' % firmware) softdevice_hex = glob.glob(firmware_dir + '/softdevice/*' + softdevice + '_softdevice.hex')[0].replace('\\', '/') bootloader_hex = glob.glob(firmware_dir + '/bootloader/*' + str(bootloader) + '.hex')[0].replace('\\', '/') signature_hex = glob.glob(firmware_dir + '/' + firmware + '/' + board + '/*_signature.hex')[0].replace('\\', '/') firmware_hex = signature_hex.replace('_signature.hex', '.hex') click.echo('Writing Softdevice + DFU bootloader + Application to flash memory') if (jtag == 'jlink') or (jtag == 'stlink'): # click.echo('adalink -v nrf51822 --programmer ' + jtag + # ' --wipe --program-hex "' + softdevice_hex + '"' + # ' --program-hex "' + bootloader_hex + '"' + # ' --program-hex "' + firmware_hex + '"' + # ' --program-hex "' + signature_hex + '"') flash_status = subprocess.call('adalink nrf51822 --programmer ' + jtag + ' --wipe --program-hex "' + softdevice_hex + '"' + ' --program-hex "' + bootloader_hex + '"' + ' --program-hex "' + firmware_hex + '"' + ' --program-hex "' + signature_hex + '"', shell=True) elif (jtag == 'rpigpio'): # or (jtag == 'stlink'): if (jtag == 'rpigpio'): if (Platform.platform_detect() != Platform.RASPBERRY_PI): sys.exit() else: openocd_bin = openocd_dict['RPi_gpio'] subprocess.call('chmod 755 ' + openocd_bin, shell=True) interface_cfg = 'raspberrypi' + ('2' if Platform.pi_version() == 2 else '') + '-native.cfg' interface_cfg = interface_cfg + ' -c "transport select swd" -c "set WORKAREASIZE 0"' else: interface_cfg = 'stlink-v2.cfg' if platform.system() != 'Linux': openocd_bin = openocd_dict[platform.system()] else: if Platform.platform_detect() == Platform.RASPBERRY_PI: openocd_bin = openocd_dict['RPi'] else: openocd_bin = openocd_dict['Ubuntu'] subprocess.call('chmod 755 ' + openocd_bin, shell=True) interface_cfg = 'stlink-v2.cfg' openocd_cmd = openocd_bin + ' -s ' + openocd_dir + '/scripts -l log.txt ' + '-f interface/' + interface_cfg + ' -f target/nrf51.cfg' flash_status = subprocess.call(openocd_cmd + ' -c init -c "reset init" -c halt -c "nrf51 mass_erase"' + ' -c "program ' + softdevice_hex + ' verify"' + ' -c "program ' + bootloader_hex + ' verify"' + ' -c "program ' + firmware_hex + ' verify"' + ' -c "program ' + signature_hex + ' verify"' + ' -c reset -c exit', shell=True if platform.system() != 'Windows' else False) else: click.echo('unsupported debugger') sys.exit() if flash_status != 0: print "Flash FAILED" else: print "Flash OK"
def flash_nrf51(jtag, softdevice, bootloader, board, firmware): """Flash Bluefruit module Softdevice + Bootloader + Firmware""" if (board is None) or (board not in supported_boards): click.echo("Please specify the board either " + ', '.join(supported_boards)) sys.exit(1) if (board == "blefriend16"): board = "blefriend" bootloader = 0 # force legacy bootloader if firmware is None: click.echo("Please specify the firmware version e.g: 0.6.5") sys.exit(1) if jtag not in supported_programmers: click.echo( "Unsupported programmer, please specify one of following: " + ', '.join(supported_programmers)) sys.exit(1) click.echo('jtag \t: %s' % jtag) click.echo('softdevice \t: %s' % softdevice) click.echo('bootloader \t: %s' % bootloader) click.echo('board \t: %s' % board) click.echo('firmware \t: %s' % firmware) softdevice_hex = glob.glob(firmware_dir + '/softdevice/*' + softdevice + '_softdevice.hex')[0].replace('\\', '/') bootloader_hex = glob.glob(firmware_dir + '/bootloader/*' + str(bootloader) + '.hex')[0].replace('\\', '/') signature_hex = glob.glob(firmware_dir + '/' + firmware + '/' + board + '/*_signature.hex')[0].replace('\\', '/') firmware_hex = signature_hex.replace('_signature.hex', '.hex') click.echo( 'Writing Softdevice + DFU bootloader + Application to flash memory') if (jtag == 'jlink') or (jtag == 'stlink'): # click.echo('adalink -v nrf51822 --programmer ' + jtag + # ' --wipe --program-hex "' + softdevice_hex + '"' + # ' --program-hex "' + bootloader_hex + '"' + # ' --program-hex "' + firmware_hex + '"' + # ' --program-hex "' + signature_hex + '"') flash_status = subprocess.call( 'adalink nrf51822 --programmer ' + jtag + ' --wipe --program-hex "' + softdevice_hex + '"' + ' --program-hex "' + bootloader_hex + '"' + ' --program-hex "' + firmware_hex + '"' + ' --program-hex "' + signature_hex + '"', shell=True) elif (jtag == 'rpigpio'): # or (jtag == 'stlink'): if (jtag == 'rpigpio'): if (Platform.platform_detect() != Platform.RASPBERRY_PI): sys.exit() else: openocd_bin = openocd_dict['RPi_gpio'] subprocess.call('chmod 755 ' + openocd_bin, shell=True) interface_cfg = 'raspberrypi' + ('2' if Platform.pi_version() == 2 else '') + '-native.cfg' interface_cfg = interface_cfg + ' -c "transport select swd" -c "set WORKAREASIZE 0"' else: interface_cfg = 'stlink-v2.cfg' if platform.system() != 'Linux': openocd_bin = openocd_dict[platform.system()] else: if Platform.platform_detect() == Platform.RASPBERRY_PI: openocd_bin = openocd_dict['RPi'] else: openocd_bin = openocd_dict['Ubuntu'] subprocess.call('chmod 755 ' + openocd_bin, shell=True) interface_cfg = 'stlink-v2.cfg' openocd_cmd = openocd_bin + ' -s ' + openocd_dir + '/scripts -l log.txt ' + '-f interface/' + interface_cfg + ' -f target/nrf51.cfg' flash_status = subprocess.call( openocd_cmd + ' -c init -c "reset init" -c halt -c "nrf51 mass_erase"' + ' -c "program ' + softdevice_hex + ' verify"' + ' -c "program ' + bootloader_hex + ' verify"' + ' -c "program ' + firmware_hex + ' verify"' + ' -c "program ' + signature_hex + ' verify"' + ' -c reset -c exit', shell=True if platform.system() != 'Windows' else False) else: click.echo('unsupported debugger') sys.exit() if flash_status != 0: print "Flash FAILED" else: print "Flash OK"