Exemplo n.º 1
0
def pinMode(gpio_pin, direction, pull=0, preserve_mode_on_exit=False):
  """ Sets given digital pin to input if direction=1, output otherwise.
      'pull' will set the pull up/down resistor if setting as an input:
      pull=-1 for pull-down, pull=1 for pull up, pull=0 for none. 
      If preserve_mode_on_exit=True, the DT overlay and will remain 
      loaded, the pin will remain exported to user-space control, and 
      the INPUT/OUTPUT mode will be preserved when the program exits. """
  assert (gpio_pin in GPIO), "*Invalid GPIO pin: '%s'" % gpio_pin
  if pinmux.export(gpio_pin) and preserve_mode_on_exit:
    addToCleanup(lambda: pinmux.unexport(gpio_pin))

  gpio_num = GPIO[gpio_pin][4]
  direction_file = '%s/direction' % (GPIO_FILE_BASE + 'gpio%i' % gpio_num)

  if (direction == INPUT):
    # Pinmux:
    if (pull > 0): pull = CONF_PULLUP
    elif (pull < 0): pull = CONF_PULLDOWN
    else: pull = CONF_PULL_DISABLE
    pinmux.pinMux(GPIO[gpio_pin][2], CONF_GPIO_INPUT | pull, 
                  preserve_mode_on_exit)
    # Set input:
    with open(direction_file, 'wb') as f:
      f.write('in')
    return
  # Pinmux:
  pinmux.pinMux(GPIO[gpio_pin][2], CONF_GPIO_OUTPUT,
                preserve_mode_on_exit)
  # Set output:
  with open(direction_file, 'wb') as f:
    f.write('out')
Exemplo n.º 2
0
def pwmEnable(pwm_pin):
  """ Ensures PWM module for the given pin is enabled and its ocp helper
      is loaded. """
  global PWM_PINS_ENABLED
  if PWM_PINS_ENABLED.get(pwm_pin): return
  pin_config = PWM_PINS[pwm_pin]
  assert (pin_config), "*Invalid PWM pin: '%s'" % pwm_pin

  for overlay in pin_config[2]:
    cape_manager.load(overlay, auto_unload=False)
    delay(250) # Give it some time to take effect
  cape_manager.load(pin_config[0], auto_unload=False)
  delay(250)

  helper_path = pin_config[1]
  # Make sure output is disabled, so it won't start outputing a 
  # signal until analogWrite() is called: 
  if (sysfs.kernelFilenameIO('%s/%s' % (helper_path, PWM_RUN)) == '1\n'):
    sysfs.kernelFilenameIO('%s/%s' % (helper_path, PWM_RUN), '0')

  # Duty cyle must be set to 0 before changing frequency:
  sysfs.kernelFilenameIO('%s/%s' % (helper_path, PWM_DUTY), '0')
  # Is this still true??

  sysfs.kernelFilenameIO('%s/%s' % (helper_path, PWM_PERIOD), 
                      str(PWM_DEFAULT_PERIOD))
  addToCleanup(lambda : pwmDisable(pwm_pin))
  PWM_PINS_ENABLED[pwm_pin] = True
Exemplo n.º 3
0
Arquivo: pwm.py Projeto: sintrb/PyBBIO
def pwmEnable(pwm_pin):
    """ Ensures PWM module for the given pin is enabled and its ocp helper
      is loaded. """
    global PWM_PINS_ENABLED
    if PWM_PINS_ENABLED.get(pwm_pin): return
    pin_config = PWM_PINS[pwm_pin]
    assert (pin_config), "*Invalid PWM pin: '%s'" % pwm_pin

    for overlay in pin_config[2]:
        cape_manager.load(overlay, auto_unload=False)
        delay(250)  # Give it some time to take effect
    cape_manager.load(pin_config[0], auto_unload=False)
    delay(250)

    helper_path = pin_config[1]
    # Make sure output is disabled, so it won't start outputing a
    # signal until analogWrite() is called:
    if (sysfs.kernelFilenameIO('%s/%s' % (helper_path, PWM_RUN)) == '1\n'):
        sysfs.kernelFilenameIO('%s/%s' % (helper_path, PWM_RUN), '0')

    # Duty cyle must be set to 0 before changing frequency:
    sysfs.kernelFilenameIO('%s/%s' % (helper_path, PWM_DUTY), '0')
    # Is this still true??

    sysfs.kernelFilenameIO('%s/%s' % (helper_path, PWM_PERIOD),
                           str(PWM_DEFAULT_PERIOD))
    addToCleanup(lambda: pwmDisable(pwm_pin))
    PWM_PINS_ENABLED[pwm_pin] = True
Exemplo n.º 4
0
def load(overlay, auto_unload=True):
    """ Attempt to load an overlay with the given name. If auto_unload=True it
      will be auto-unloaded at program exit (the current cape manager crashes
      when trying to unload certain overlay fragments). """
    with open(SLOTS_FILE, 'rb') as f:
        capes = f.read()
    if overlay in capes:
        # already loaded (this should do a better job checking)
        return
    with open(SLOTS_FILE, 'wb') as f:
        f.write(overlay)
    if auto_unload:
        addToCleanup(lambda: unload(overlay))
Exemplo n.º 5
0
def load(overlay, auto_unload=True):
  """ Attempt to load an overlay with the given name. If auto_unload=True it
      will be auto-unloaded at program exit (the current cape manager crashes
      when trying to unload certain overlay fragments). """
  with open(SLOTS_FILE, 'rb') as f:
    capes = f.read()
  if overlay in capes:
    # already loaded (this should do a better job checking)
    return
  with open(SLOTS_FILE, 'wb') as f:
    f.write(overlay)
  if auto_unload:
    addToCleanup(lambda: unload(overlay))
Exemplo n.º 6
0
def pinMode(gpio_pin, direction, pull=0):
  """ Sets given digital pin to input if direction=1, output otherwise.
      'pull' will set the pull up/down resistor if setting as an input:
      pull=-1 for pull-down, pull=1 for pull up, pull=0 for none. """
  assert (gpio_pin in GPIO), "*Invalid GPIO pin: '%s'" % gpio_pin
  if pinmux.export(gpio_pin):
    addToCleanup(lambda: _unexport(gpio_pin))
  if (direction == INPUT):
    # Pinmux:
    if (pull > 0): pull = CONF_PULLUP
    elif (pull < 0): pull = CONF_PULLDOWN
    else: pull = CONF_PULL_DISABLE
    pinmux.pinMux(GPIO[gpio_pin][2], CONF_GPIO_INPUT | pull)
    # Set input:
    memory.orReg(GPIO[gpio_pin][0]+GPIO_OE, GPIO[gpio_pin][1])
    return
  # Pinmux:
  pinmux.pinMux(GPIO[gpio_pin][2], CONF_GPIO_OUTPUT)
  # Set output:
  memory.clearReg(GPIO[gpio_pin][0]+GPIO_OE, GPIO[gpio_pin][1])
Exemplo n.º 7
0
def pinMode(gpio_pin, direction, pull=0):
  """ Sets given digital pin to input if direction=1, output otherwise.
      'pull' will set the pull up/down resistor if setting as an input:
      pull=-1 for pull-down, pull=1 for pull up, pull=0 for none. """
  assert (gpio_pin in GPIO), "*Invalid GPIO pin: '%s'" % gpio_pin
  if pinmux.export(gpio_pin):
    addToCleanup(lambda: pinmux.unexport(gpio_pin))
  if (direction == INPUT):
    # Pinmux:
    if (pull > 0): pull = CONF_PULLUP
    elif (pull < 0): pull = CONF_PULLDOWN
    else: pull = CONF_PULL_DISABLE
    pinmux.pinMux(GPIO[gpio_pin][2], CONF_GPIO_INPUT | pull)
    # Set input:
    memory.orReg(GPIO[gpio_pin][0]+GPIO_OE, GPIO[gpio_pin][1])
    return
  # Pinmux:
  pinmux.pinMux(GPIO[gpio_pin][2], CONF_GPIO_OUTPUT)
  # Set output:
  memory.clearReg(GPIO[gpio_pin][0]+GPIO_OE, GPIO[gpio_pin][1])
Exemplo n.º 8
0
def pinMode(gpio_pin, direction, pull=0, preserve_mode_on_exit=False):
    """ Sets given digital pin to input if direction=1, output otherwise.
      'pull' will set the pull up/down resistor if setting as an input:
      pull=-1 for pull-down, pull=1 for pull up, pull=0 for none. 
      If preserve_mode_on_exit=True, the DT overlay and will remain 
      loaded, the pin will remain exported to user-space control, and 
      the INPUT/OUTPUT mode will be preserved when the program exits. """

    if 'USR' in gpio_pin:
        if direction == INPUT:
            print 'warning: cannot set USR LEDs to INPUT'
        return
    assert (gpio_pin in GPIO), "*Invalid GPIO pin: '%s'" % gpio_pin
    exported = pinmux.export(gpio_pin)
    if not exported:
        print "warning: could not export pin '%s', skipping pinMode()" % gpio_pin
        return
    elif not preserve_mode_on_exit:
        addToCleanup(lambda: pinmux.unexport(gpio_pin))

    direction_file = getGPIODirectionFile(gpio_pin)

    if (direction == INPUT):
        # Pinmux:
        if (pull > 0): pull = CONF_PULLUP
        elif (pull < 0): pull = CONF_PULLDOWN
        else: pull = CONF_PULL_DISABLE
        pinmux.pinMux(gpio_pin, CONF_GPIO_INPUT | pull, preserve_mode_on_exit)
        # Set input:
        with open(direction_file, 'wb') as f:
            f.write('in')
        return
    # Pinmux:
    pinmux.pinMux(gpio_pin, CONF_GPIO_OUTPUT, preserve_mode_on_exit)
    # Set output:
    with open(direction_file, 'wb') as f:
        f.write('out')
Exemplo n.º 9
0
def pinMode(gpio_pin, direction, pull=0, preserve_mode_on_exit=False):
  """ Sets given digital pin to input if direction=1, output otherwise.
      'pull' will set the pull up/down resistor if setting as an input:
      pull=-1 for pull-down, pull=1 for pull up, pull=0 for none. 
      If preserve_mode_on_exit=True, the DT overlay and will remain 
      loaded, the pin will remain exported to user-space control, and 
      the INPUT/OUTPUT mode will be preserved when the program exits. """

  if 'USR' in gpio_pin:
    if direction == INPUT:
      print 'warning: cannot set USR LEDs to INPUT'
    return
  assert (gpio_pin in GPIO), "*Invalid GPIO pin: '%s'" % gpio_pin
  exported = pinmux.export(gpio_pin)
  if not exported:
    print "warning: could not export pin '%s', skipping pinMode()" % gpio_pin
    return
  elif not preserve_mode_on_exit:
    addToCleanup(lambda: pinmux.unexport(gpio_pin))

  direction_file = getGPIODirectionFile(gpio_pin)

  if (direction == INPUT):
    # Pinmux:
    if (pull > 0): pull = CONF_PULLUP
    elif (pull < 0): pull = CONF_PULLDOWN
    else: pull = CONF_PULL_DISABLE
    pinmux.pinMux(gpio_pin, CONF_GPIO_INPUT | pull, preserve_mode_on_exit)
    # Set input:
    with open(direction_file, 'wb') as f:
      f.write('in')
    return
  # Pinmux:
  pinmux.pinMux(gpio_pin, CONF_GPIO_OUTPUT, preserve_mode_on_exit)
  # Set output:
  with open(direction_file, 'wb') as f:
    f.write('out')