[9f1646]: cp210x_gpio.py Maximize Restore History

Download this file

cp210x_gpio.py    67 lines (56 with data), 2.5 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
#-----------------------------------------------------------------------------
# FILE NAME : cp210x_gpio.py
# DESCRIPTION: control GPIO pins for CP210X USB to UART Bridges
# Copyright (C) 2013 Elphel, Inc
#-----------------------------------------------------------------------------
# This program 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.
#
# This program 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.
#
# The four essential freedoms with GNU GPL software are:
# * to run the program for any purpose
# * to study how the program works and change it to make it do what you wish
# * to redistribute copies so you can help your neighbor
# * to distribute copies of your modified versions to others
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#-----------------------------------------------------------------------------
__author__ = "Oleg Dzhimiev"
__copyright__ = "Copyright 2013, Elphel, Inc."
__license__ = "GPL"
__version__ = "3.0+"
__maintainer__ = "Oleg Dzhimiev"
__email__ = "oleg@elphel.com"
__status__ = "Released"
import array, fcntl
import argparse # http://docs.python.org/2/howto/argparse.html
IOCTL_GPIOGET = 0x8000
IOCTL_GPIOSET = 0x8001
parser = argparse.ArgumentParser()
parser.add_argument('-d', '--device', default="/dev/ttyUSB0", help='USB device. Default: /dev/ttyUSB0')
parser.add_argument("gpio_value", help="hex value for GPIO. Example (GPIO[3:0]): 0xf - all 1's, 0x0 - all 0's")
parser.add_argument('-m', '--mask', default="0xff",help='hex value for masking out GPIO bits: 1 - enable rewrite, 0 - disable. Example (GPIO[3:0]): -m 0xf')
args = parser.parse_args()
device=args.device
mask = int(args.mask,0)
gpio = (int(args.gpio_value,0)<<16)|mask
print ('Target GPIO value:'),gpio
try:
fd = open(device, 'r+')
except:
raise Exception(str(device)+' is not present')
buf = array.array('l', [0])
fcntl.ioctl(fd, IOCTL_GPIOGET, buf, 1)
print ('Old GPIO value:'),hex(buf[0])
buf[0] = gpio
fcntl.ioctl(fd, IOCTL_GPIOSET, buf, 1)
fcntl.ioctl(fd, IOCTL_GPIOGET, buf, 1)
print ('New GPIO value:'),hex(buf[0])