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')
def uartInit(uart): """ Muxes given serial port's header pins for use. Returns True if successful, False otherwise. """ tx_pinmux_filename = UART[uart.config][1] tx_pinmux_mode = UART[uart.config][2] | CONF_UART_TX pinmux.pinMux(tx_pinmux_filename, tx_pinmux_mode) rx_pinmux_filename = UART[uart.config][3] rx_pinmux_mode = UART[uart.config][4] | CONF_UART_RX pinmux.pinMux(rx_pinmux_filename, rx_pinmux_mode) # Not catching errors for now, not sure what could go wrong here... return True
def begin(self, baud, timeout=1): """ Starts the serial port at the given baud rate. """ # Set proper pinmux to match expansion headers: tx_pinmux_filename = UART[self.config][1] tx_pinmux_mode = UART[self.config][2] | CONF_UART_TX pinmux.pinMux(tx_pinmux_filename, tx_pinmux_mode) rx_pinmux_filename = UART[self.config][3] rx_pinmux_mode = UART[self.config][4] | CONF_UART_RX pinmux.pinMux(rx_pinmux_filename, rx_pinmux_mode) port = UART[self.config][0] self.baud = baud self.ser_port = serial.Serial(port, baud, timeout=timeout) self.open = True
def pwmEnable(pwm_pin): """ Ensures given PWM output is reserved for userspace use and sets proper pinmux. Sets frequency to default value if output not already reserved. """ assert (pwm_pin in PWM_PINS), "*Invalid PWM pin: '%s'" % pwm_pin # Set pinmux mode: pinmux.pinMux(PWM_PINS[pwm_pin][0], PWM_PINS[pwm_pin][1]) if ('sysfs' not in pinmux.kernelFileIO(PWM_FILES[pwm_pin][PWM_REQUEST])): # Reserve use of output: pinmux.kernelFileIO(PWM_FILES[pwm_pin][PWM_REQUEST], '1') delay(1) # Give it some time to take effect # Make sure output is disabled, so it won't start outputing a # signal until analogWrite() is called: if (pinmux.kernelFileIO(PWM_FILES[pwm_pin][PWM_ENABLE]) == '1\n'): pinmux.kernelFileIO(PWM_FILES[pwm_pin][PWM_ENABLE], '0') # Duty cyle must be set to 0 before changing frequency: pinmux.kernelFileIO(PWM_FILES[pwm_pin][PWM_DUTY], '0') # Set frequency to default: pinmux.kernelFileIO(PWM_FILES[pwm_pin][PWM_FREQ], str(PWM_DEFAULT_FREQ))
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])
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])
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: print 'warning: pinMode() not supported for USR LEDs' 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 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')