def pinMux(register_name, mode, preserve_mode_on_exit=False): """ Uses custom device tree overlays to set pin modes. If preserve_mode_on_exit=True the overlay will remain loaded when the program exits, otherwise it will be unloaded before exiting. *This should generally not be called directly from user code. """ gpio_pin = '' for pin, config in GPIO.items(): if config[2] == register_name: gpio_pin = pin.lower() break if not gpio_pin: print "*unknown pinmux register: %s" % register_name return mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: cape_manager.load('PyBBIO-%s' % gpio_pin, not preserve_mode_on_exit) mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: print "*Could not load overlay for pin: %s" % gpio_pin return mux_file = mux_file_glob[0] # Convert mode to ocp mux name: mode = 'mode_%s' % format(mode, '#010b') # Possible modes: # mode_0b00100111 # rx active | pull down # mode_0b00110111 # rx active | pull up # mode_0b00101111 # rx active | no pull # mode_0b00000111 # pull down # mode_0b00010111 # pull up # mode_0b00001111 # no pull # See /lib/firmware/PyBBIO-src/*.dts for more info with open(mux_file, 'wb') as f: f.write(mode)
def open(self, use_10bit_address=False): """ I2CDev.open(use_10bit_address=False) -> None Initialize the I2C bus interface. If use_10bit_address=True the bus will use 10-bit slave addresses instead of 7-bit addresses. """ if self._is_open: return if self.bus_num == 1 and not cape_manager.isLoaded("BB-I2C1"): # I2C2 is already enabled for reading cape EEPROMs, # so only need to load overlay for I2C1 cape_manager.load("BB-I2C1", auto_unload=False) bbio.common.delay(10) # Make sure it initialized correctly: if not cape_manager.isLoaded("BB-I2C1"): raise IOError("could not enable I2C1") # Detect bus number: # (since I2C2 is always enabled at boot, it's kernel assigned # bus number will always be the same, and I2C1 will always be # the next consecutive bus number, so this isn't actually # required) for i in glob.glob("/sys/bus/i2c/devices/i2c-*"): path = os.path.realpath(i) module_addr = int(path.split("/")[4].split(".")[0], 16) if module_addr == I2C_BASE_ADDRESSES[self.bus_num]: self.bus_num = int(path.split("/")[5][-1]) break super(I2CBus, self).open(use_10bit_address=use_10bit_address) self._is_open = True
def pinMux(register_name, mode): """ Uses custom device tree overlays to set pin modes. """ gpio_pin = '' for pin, config in GPIO.items(): if config[2] == register_name: gpio_pin = pin.lower() break if not gpio_pin: print "*unknown pinmux register: %s" % register_name return mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: cape_manager.load('PyBBIO-%s' % gpio_pin) mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: print "*Could not load overlay for pin: %s" % gpio_pin return mux_file = mux_file_glob[0] # Convert mode to ocp mux name: mode = 'mode_%s' % format(mode, '#010b') # Possible modes: # mode_0b00100111 # rx active | pull down # mode_0b00110111 # rx active | pull up # mode_0b00101111 # rx active | no pull # mode_0b00000111 # pull down # mode_0b00010111 # pull up # mode_0b00001111 # no pull # See /lib/firmware/PyBBIO-src/*.dts for more info with open(mux_file, 'wb') as f: f.write(mode)
def analogRead(adc_pin): """ Returns voltage read on given analog input pin. If passed one of PyBBIO's AIN0-AIN5 keywords the voltage will be returned in millivolts. May also be passed the path to an AIN file as created by a cape overlay, in which case the value will be returned as found in the file. """ global _ADC_INITIALIZED if not _ADC_INITIALIZED: cape_manager.load(ADC_ENABLE_DTS_OVERLAY, auto_unload=False) # Don't unload the overlay on exit for now because it can # cause kernel panic. bbio.delay(100) _ADC_INITIALIZED = True if adc_pin in ADC: adc_pin = ADC[adc_pin] adc_file = glob.glob(adc_pin[0]) if len(adc_file) == 0: overlay = adc_pin[1] # Overlay not loaded yet cape_manager.load(overlay, auto_unload=False) adc_file = glob.glob(adc_pin[0]) else: adc_file = glob.glob(adc_pin) if len(adc_file) == 0: raise Exception('*Could load overlay for adc_pin: %s' % adc_pin) adc_file = adc_file[0] # Occasionally the kernel will be writing to the file when you try # to read it, to avoid IOError try up to 5 times: for i in range(5): try: with open(adc_file, 'rb') as f: val = f.read() return int(val) except IOError: continue raise Exception('*Could not open AIN file: %s' % adc_file)
def analogRead(adc_pin): """ Returns voltage read on given analog input pin. If passed one of PyBBIO's AIN0-AIN5 keywords the voltage will be returned in millivolts. May also be passed the path to an AIN file as created by a cape overlay, in which case the value will be returned as found in the file. """ global _ADC_INITIALIZED if not _ADC_INITIALIZED: cape_manager.load(ADC_ENABLE_DTS_OVERLAY, auto_unload=False) # Don't unload the overlay on exit for now because it can # cause kernel panic. bbio.delay(100) _ADC_INITIALIZED = True if adc_pin in ADC: adc_pin = ADC[adc_pin] adc_file = glob.glob(adc_pin[0]) if len(adc_file) == 0: overlay = adc_pin[1] # Overlay not loaded yet cape_manager.load(overlay, auto_unload=False) adc_file = glob.glob(adc_pin[0]) else: adc_file = glob.glob(adc_pin) if len(adc_file) == 0: raise Exception('*Could not load overlay for adc_pin: %s' % adc_pin) adc_file = adc_file[0] # Occasionally the kernel will be writing to the file when you try # to read it, to avoid IOError try up to 5 times: for i in range(5): try: with open(adc_file, 'rb') as f: val = f.read() return int(val) except IOError: continue raise Exception('*Could not open AIN file: %s' % adc_file)
def analogRead(adc_pin): """ Returns voltage read on given analog input pin in millivolts. """ if adc_pin in ADC: adc_pin = ADC[adc_pin] adc_file = adc_pin[0] if not os.path.exists(adc_file): # Overlay not loaded yet overlay = adc_pin[1] cape_manager.load(overlay, auto_unload=False) with open(glob.glob(adc_file)[0], 'rb') as f: mv = f.read() return int(mv)
def open(self): overlay = 'BB-SPIDEV%i' % (self.bus-1) cape_manager.load(overlay, auto_unload=False) bbio.common.delay(250) # Give driver time to load assert cape_manager.isLoaded(overlay), "Could not load SPI overlay" super(SPIBus, self).open() # Initialize to default parameters: self.setCSActiveLow(0) self.setBitsPerWord(0, 8) self.setMaxFrequency(0, 8000000) self.setClockMode(0, 0)
def open(self, use_10bit_address=False): """ I2CDev.open(use_10bit_address=False) -> None Initialize the I2C bus interface. If use_10bit_address=True the bus will use 10-bit slave addresses instead of 7-bit addresses. """ if not os.path.exists('/dev/i2c-%i' % self.bus_num): cape_manager.load('BB-I2C%i' % self.hw_bus, auto_unload=False) bbio.common.delay(10) # Make sure it initialized correctly: assert os.path.exists('/dev/i2c-%i' % self.bus_num), \ 'could not enable I2C bus %i' % self.hw_bus super(I2CBus, self).open(use_10bit_address=use_10bit_address)
def pinMux(gpio_pin, mode, preserve_mode_on_exit=False): """ Uses custom device tree overlays to set pin modes. If preserve_mode_on_exit=True the overlay will remain loaded when the program exits, otherwise it will be unloaded before exiting. *This should generally not be called directly from user code. """ gpio_pin = gpio_pin.lower() if not gpio_pin: print "*unknown pinmux pin: %s" % gpio_pin return mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: try: cape_manager.load('PyBBIO-%s' % gpio_pin, not preserve_mode_on_exit) bbio.delay(250) # Give driver time to load except IOError: print "*Could not load %s overlay, resource busy" % gpio_pin return mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: print "*Could not load overlay for pin: %s" % gpio_pin return mux_file = mux_file_glob[0] # Convert mode to ocp mux name: mode = 'mode_%s' % format(mode, '#010b') # Possible modes: # mode_0b00100111 # rx active | pull down # mode_0b00110111 # rx active | pull up # mode_0b00101111 # rx active | no pull # mode_0b00000111 # pull down # mode_0b00010111 # pull up # mode_0b00001111 # no pull # See /lib/firmware/PyBBIO-src/*.dts for more info for i in range(3): # If the pin's overlay was just loaded there may not have been enough # time for the driver to get fully initialized, which causes an IOError # when trying to write the mode; try up to 3 times to avoid this: try: with open(mux_file, 'wb') as f: f.write(mode) return except IOError: # Wait a bit between attempts bbio.delay(10) # If we get here then it didn't work 3 times in a row; raise the IOError: raise
def pinMux(gpio_pin, mode, preserve_mode_on_exit=False): """ Uses custom device tree overlays to set pin modes. If preserve_mode_on_exit=True the overlay will remain loaded when the program exits, otherwise it will be unloaded before exiting. *This should generally not be called directly from user code. """ gpio_pin = gpio_pin.lower() if not gpio_pin: print "*unknown pinmux pin: %s" % gpio_pin return mux_file_glob = glob.glob("%s/*%s*/state" % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: try: cape_manager.load("PyBBIO-%s" % gpio_pin, not preserve_mode_on_exit) bbio.delay(250) # Give driver time to load except IOError: print "*Could not load %s overlay, resource busy" % gpio_pin return mux_file_glob = glob.glob("%s/*%s*/state" % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: print "*Could not load overlay for pin: %s" % gpio_pin return mux_file = mux_file_glob[0] # Convert mode to ocp mux name: mode = "mode_%s" % format(mode, "#010b") # Possible modes: # mode_0b00100111 # rx active | pull down # mode_0b00110111 # rx active | pull up # mode_0b00101111 # rx active | no pull # mode_0b00000111 # pull down # mode_0b00010111 # pull up # mode_0b00001111 # no pull # See /lib/firmware/PyBBIO-src/*.dts for more info for i in range(3): # If the pin's overlay was just loaded there may not have been enough # time for the driver to get fully initialized, which causes an IOError # when trying to write the mode; try up to 3 times to avoid this: try: with open(mux_file, "wb") as f: f.write(mode) return except IOError: # Wait a bit between attempts bbio.delay(10) # If we get here then it didn't work 3 times in a row; raise the IOError: raise
def uartInit(uart): """ Enables the given uart by loading its dto. """ port, overlay = UART[uart] if os.path.exists(port): return True # Unloading serial port overlays crashes the current cape manager, # disable until it gets fixed: cape_manager.load(overlay, auto_unload=False) if os.path.exists(port): return True for i in range(5): # Give it some time to load bbio.delay(100) if os.path.exists(port): return True # If we make it here it's pretty safe to say the overlay couldn't load return False
def open(self): overlay = "BB-SPIDEV%i" % (self.bus) cape_manager.load(overlay, auto_unload=False) bbio.common.delay(250) # Give driver time to load assert cape_manager.isLoaded(overlay), "Could not load SPI overlay" for i in glob.glob("/sys/bus/spi/devices/*.0"): path = os.path.realpath(i) module_addr = int(path.split("/")[4].split(".")[0], 16) if module_addr == SPI_BASE_ADDRESSES[self.bus]: self.bus = int(path.split("/")[6][-1]) break super(SPIBus, self).open() # Initialize to default parameters: self.setCSActiveLow(0) self.setBitsPerWord(0, 8) self.setMaxFrequency(0, 8000000) self.setClockMode(0, 0)
def analogRead(adc_pin): """ Returns voltage read on given analog input pin in millivolts. """ if adc_pin in ADC: adc_pin = ADC[adc_pin] adc_file = adc_pin[0] if not os.path.exists(adc_file): # Overlay not loaded yet overlay = adc_pin[1] cape_manager.load(overlay, auto_unload=False) # Occasionally the kernel will be writing to the file when you try # to read it, to avoid IOError try up to 5 times: for i in range(5): try: with open(glob.glob(adc_file)[0], 'rb') as f: mv = f.read() return int(mv) except IOError: continue raise Exception('*Could not open AIN file: %s' % adc_file)
def i2cInit(bus): """ Initializes reqd I2C bus i2c0 (/dev/i2c-0) and i2c2 (/dev/i2c-1) are already initialized overlay to be applied for i2c1 (/dev/i2c-2) """ dev_file, overlay = I2C[bus] if os.path.exists(dev_file): return True cape_manager.load(overlay, auto_unload=False) if os.path.exists(dev_file): return True for i in range(5): bbio.delay(5) if os.path.exists(dev_file): return True return False
def i2cInit(bus): ''' Initializes reqd I2C bus i2c0 (/dev/i2c-0) and i2c2 (/dev/i2c-1) are already initialized overlay to be applied for i2c1 (/dev/i2c-2) ''' dev_file, overlay = I2C[bus] if os.path.exists(dev_file): return True cape_manager.load(overlay, auto_unload=False) if os.path.exists(dev_file): return True for i in range(5): bbio.delay(5) if os.path.exists(dev_file): return True return False
def pinMux_dtOverlays(gpio_pin, mode, preserve_mode_on_exit=False): gpio_pin = gpio_pin.lower() mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: try: cape_manager.load('PyBBIO-%s' % gpio_pin, not preserve_mode_on_exit) bbio.delay(250) # Give driver time to load mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) except IOError: print "*Could not load %s overlay, resource busy" % gpio_pin return False if len(mux_file_glob) == 0: print "*Could not load overlay for pin: %s" % gpio_pin return False mux_file = mux_file_glob[0] # Convert mode to ocp mux name: mode = 'mode_%s' % format(mode, '#010b') # Possible modes: # mode_0b00100111 # rx active | pull down # mode_0b00110111 # rx active | pull up # mode_0b00101111 # rx active | no pull # mode_0b00000111 # pull down # mode_0b00010111 # pull up # mode_0b00001111 # no pull # See /lib/firmware/PyBBIO-src/*.dts for more info for i in range(3): # If the pin's overlay was just loaded there may not have been enough # time for the driver to get fully initialized, which causes an IOError # when trying to write the mode; try up to 3 times to avoid this: try: with open(mux_file, 'wb') as f: f.write(mode) return True except IOError: # Wait a bit between attempts bbio.delay(10) # If we get here then it didn't work 3 times in a row; raise the IOError: raise
def open(self, use_10bit_address=False): """ I2CDev.open(use_10bit_address=False) -> None Initialize the I2C bus interface. If use_10bit_address=True the bus will use 10-bit slave addresses instead of 7-bit addresses. """ if not os.path.exists('/dev/i2c-%i' % self.bus_num): cape_manager.load('BB-I2C%i' % self.hw_bus, auto_unload=False) bbio.common.delay(10) # Make sure it initialized correctly: assert os.path.exists('/dev/i2c-%i' % self.bus_num), \ 'could not enable I2C bus %i' % self.hw_bus # Detect bus number: for i in glob.glob("/sys/bus/i2c/devices/i2c-*"): path = os.path.realpath(i) module_addr = int(path.split("/")[4].split(".")[0], 16) if module_addr == I2C_BASE_ADDRESSES[self.bus_num]: self.bus_num = int(path.split("/")[5][-1]) break super(I2CBus, self).open(use_10bit_address=use_10bit_address)
def pinMux(gpio_pin, mode, preserve_mode_on_exit=False): """ Uses custom device tree overlays to set pin modes. If preserve_mode_on_exit=True the overlay will remain loaded when the program exits, otherwise it will be unloaded before exiting. *This should generally not be called directly from user code. """ gpio_pin = gpio_pin.lower() if not gpio_pin: print "*unknown pinmux pin: %s" % gpio_pin return mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: try: cape_manager.load('PyBBIO-%s' % gpio_pin, not preserve_mode_on_exit) bbio.delay(250) # Give driver time to load except IOError: print "*Could not load %s overlay, resource busy" % gpio_pin return mux_file_glob = glob.glob('%s/*%s*/state' % (OCP_PATH, gpio_pin)) if len(mux_file_glob) == 0: print "*Could not load overlay for pin: %s" % gpio_pin return mux_file = mux_file_glob[0] # Convert mode to ocp mux name: mode = 'mode_%s' % format(mode, '#010b') # Possible modes: # mode_0b00100111 # rx active | pull down # mode_0b00110111 # rx active | pull up # mode_0b00101111 # rx active | no pull # mode_0b00000111 # pull down # mode_0b00010111 # pull up # mode_0b00001111 # no pull # See /lib/firmware/PyBBIO-src/*.dts for more info with open(mux_file, 'wb') as f: f.write(mode)
def spi_init(spi_num): overlay = 'BB-SPIDEV%i' % spi_num assert os.path.exists('/lib/firmware/%s-00A0.dtbo' % overlay), \ "SPI driver not present" cape_manager.load(overlay, auto_unload=False) bbio.delay(250) # Give driver time to load
def analog_init(): """ Initializes the on-board 8ch 12bit ADC. """ cape_manager.load(ADC_ENABLE_DTS_OVERLAY, auto_unload=False)